soc: ti: pruss: Avoid cast to incompatible function type
[platform/kernel/linux-starfive.git] / drivers / soc / ti / pruss.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * PRU-ICSS platform driver for various TI SoCs
4  *
5  * Copyright (C) 2014-2020 Texas Instruments Incorporated - http://www.ti.com/
6  * Author(s):
7  *      Suman Anna <s-anna@ti.com>
8  *      Andrew F. Davis <afd@ti.com>
9  */
10
11 #include <linux/clk-provider.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/io.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/pruss_driver.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22
23 /**
24  * struct pruss_private_data - PRUSS driver private data
25  * @has_no_sharedram: flag to indicate the absence of PRUSS Shared Data RAM
26  * @has_core_mux_clock: flag to indicate the presence of PRUSS core clock
27  */
28 struct pruss_private_data {
29         bool has_no_sharedram;
30         bool has_core_mux_clock;
31 };
32
33 static void pruss_of_free_clk_provider(void *data)
34 {
35         struct device_node *clk_mux_np = data;
36
37         of_clk_del_provider(clk_mux_np);
38         of_node_put(clk_mux_np);
39 }
40
41 static void pruss_clk_unregister_mux(void *data)
42 {
43         clk_unregister_mux(data);
44 }
45
46 static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux,
47                                char *mux_name, struct device_node *clks_np)
48 {
49         struct device_node *clk_mux_np;
50         struct device *dev = pruss->dev;
51         char *clk_mux_name;
52         unsigned int num_parents;
53         const char **parent_names;
54         void __iomem *reg;
55         u32 reg_offset;
56         int ret;
57
58         clk_mux_np = of_get_child_by_name(clks_np, mux_name);
59         if (!clk_mux_np) {
60                 dev_err(dev, "%pOF is missing its '%s' node\n", clks_np,
61                         mux_name);
62                 return -ENODEV;
63         }
64
65         num_parents = of_clk_get_parent_count(clk_mux_np);
66         if (num_parents < 1) {
67                 dev_err(dev, "mux-clock %pOF must have parents\n", clk_mux_np);
68                 ret = -EINVAL;
69                 goto put_clk_mux_np;
70         }
71
72         parent_names = devm_kcalloc(dev, sizeof(*parent_names), num_parents,
73                                     GFP_KERNEL);
74         if (!parent_names) {
75                 ret = -ENOMEM;
76                 goto put_clk_mux_np;
77         }
78
79         of_clk_parent_fill(clk_mux_np, parent_names, num_parents);
80
81         clk_mux_name = devm_kasprintf(dev, GFP_KERNEL, "%s.%pOFn",
82                                       dev_name(dev), clk_mux_np);
83         if (!clk_mux_name) {
84                 ret = -ENOMEM;
85                 goto put_clk_mux_np;
86         }
87
88         ret = of_property_read_u32(clk_mux_np, "reg", &reg_offset);
89         if (ret)
90                 goto put_clk_mux_np;
91
92         reg = pruss->cfg_base + reg_offset;
93
94         clk_mux = clk_register_mux(NULL, clk_mux_name, parent_names,
95                                    num_parents, 0, reg, 0, 1, 0, NULL);
96         if (IS_ERR(clk_mux)) {
97                 ret = PTR_ERR(clk_mux);
98                 goto put_clk_mux_np;
99         }
100
101         ret = devm_add_action_or_reset(dev, pruss_clk_unregister_mux, clk_mux);
102         if (ret) {
103                 dev_err(dev, "failed to add clkmux unregister action %d", ret);
104                 goto put_clk_mux_np;
105         }
106
107         ret = of_clk_add_provider(clk_mux_np, of_clk_src_simple_get, clk_mux);
108         if (ret)
109                 goto put_clk_mux_np;
110
111         ret = devm_add_action_or_reset(dev, pruss_of_free_clk_provider,
112                                        clk_mux_np);
113         if (ret) {
114                 dev_err(dev, "failed to add clkmux free action %d", ret);
115                 goto put_clk_mux_np;
116         }
117
118         return 0;
119
120 put_clk_mux_np:
121         of_node_put(clk_mux_np);
122         return ret;
123 }
124
125 static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
126 {
127         const struct pruss_private_data *data;
128         struct device_node *clks_np;
129         struct device *dev = pruss->dev;
130         int ret = 0;
131
132         data = of_device_get_match_data(dev);
133
134         clks_np = of_get_child_by_name(cfg_node, "clocks");
135         if (!clks_np) {
136                 dev_err(dev, "%pOF is missing its 'clocks' node\n", cfg_node);
137                 return -ENODEV;
138         }
139
140         if (data && data->has_core_mux_clock) {
141                 ret = pruss_clk_mux_setup(pruss, pruss->core_clk_mux,
142                                           "coreclk-mux", clks_np);
143                 if (ret) {
144                         dev_err(dev, "failed to setup coreclk-mux\n");
145                         goto put_clks_node;
146                 }
147         }
148
149         ret = pruss_clk_mux_setup(pruss, pruss->iep_clk_mux, "iepclk-mux",
150                                   clks_np);
151         if (ret) {
152                 dev_err(dev, "failed to setup iepclk-mux\n");
153                 goto put_clks_node;
154         }
155
156 put_clks_node:
157         of_node_put(clks_np);
158
159         return ret;
160 }
161
162 static struct regmap_config regmap_conf = {
163         .reg_bits = 32,
164         .val_bits = 32,
165         .reg_stride = 4,
166 };
167
168 static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
169 {
170         struct device_node *np = dev_of_node(dev);
171         struct device_node *child;
172         struct resource res;
173         int ret;
174
175         child = of_get_child_by_name(np, "cfg");
176         if (!child) {
177                 dev_err(dev, "%pOF is missing its 'cfg' node\n", child);
178                 return -ENODEV;
179         }
180
181         if (of_address_to_resource(child, 0, &res)) {
182                 ret = -ENOMEM;
183                 goto node_put;
184         }
185
186         pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
187         if (!pruss->cfg_base) {
188                 ret = -ENOMEM;
189                 goto node_put;
190         }
191
192         regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
193                                      (u64)res.start);
194         regmap_conf.max_register = resource_size(&res) - 4;
195
196         pruss->cfg_regmap = devm_regmap_init_mmio(dev, pruss->cfg_base,
197                                                   &regmap_conf);
198         kfree(regmap_conf.name);
199         if (IS_ERR(pruss->cfg_regmap)) {
200                 dev_err(dev, "regmap_init_mmio failed for cfg, ret = %ld\n",
201                         PTR_ERR(pruss->cfg_regmap));
202                 ret = PTR_ERR(pruss->cfg_regmap);
203                 goto node_put;
204         }
205
206         ret = pruss_clk_init(pruss, child);
207         if (ret)
208                 dev_err(dev, "pruss_clk_init failed, ret = %d\n", ret);
209
210 node_put:
211         of_node_put(child);
212         return ret;
213 }
214
215 static int pruss_probe(struct platform_device *pdev)
216 {
217         struct device *dev = &pdev->dev;
218         struct device_node *np = dev_of_node(dev);
219         struct device_node *child;
220         struct pruss *pruss;
221         struct resource res;
222         int ret, i, index;
223         const struct pruss_private_data *data;
224         const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" };
225
226         data = of_device_get_match_data(&pdev->dev);
227
228         ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
229         if (ret) {
230                 dev_err(dev, "failed to set the DMA coherent mask");
231                 return ret;
232         }
233
234         pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL);
235         if (!pruss)
236                 return -ENOMEM;
237
238         pruss->dev = dev;
239
240         child = of_get_child_by_name(np, "memories");
241         if (!child) {
242                 dev_err(dev, "%pOF is missing its 'memories' node\n", child);
243                 return -ENODEV;
244         }
245
246         for (i = 0; i < PRUSS_MEM_MAX; i++) {
247                 /*
248                  * On AM437x one of two PRUSS units don't contain Shared RAM,
249                  * skip it
250                  */
251                 if (data && data->has_no_sharedram && i == PRUSS_MEM_SHRD_RAM2)
252                         continue;
253
254                 index = of_property_match_string(child, "reg-names",
255                                                  mem_names[i]);
256                 if (index < 0) {
257                         of_node_put(child);
258                         return index;
259                 }
260
261                 if (of_address_to_resource(child, index, &res)) {
262                         of_node_put(child);
263                         return -EINVAL;
264                 }
265
266                 pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
267                                                         resource_size(&res));
268                 if (!pruss->mem_regions[i].va) {
269                         dev_err(dev, "failed to parse and map memory resource %d %s\n",
270                                 i, mem_names[i]);
271                         of_node_put(child);
272                         return -ENOMEM;
273                 }
274                 pruss->mem_regions[i].pa = res.start;
275                 pruss->mem_regions[i].size = resource_size(&res);
276
277                 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
278                         mem_names[i], &pruss->mem_regions[i].pa,
279                         pruss->mem_regions[i].size, pruss->mem_regions[i].va);
280         }
281         of_node_put(child);
282
283         platform_set_drvdata(pdev, pruss);
284
285         pm_runtime_enable(dev);
286         ret = pm_runtime_resume_and_get(dev);
287         if (ret < 0) {
288                 dev_err(dev, "couldn't enable module\n");
289                 goto rpm_disable;
290         }
291
292         ret = pruss_cfg_of_init(dev, pruss);
293         if (ret < 0)
294                 goto rpm_put;
295
296         ret = devm_of_platform_populate(dev);
297         if (ret) {
298                 dev_err(dev, "failed to register child devices\n");
299                 goto rpm_put;
300         }
301
302         return 0;
303
304 rpm_put:
305         pm_runtime_put_sync(dev);
306 rpm_disable:
307         pm_runtime_disable(dev);
308         return ret;
309 }
310
311 static int pruss_remove(struct platform_device *pdev)
312 {
313         struct device *dev = &pdev->dev;
314
315         devm_of_platform_depopulate(dev);
316
317         pm_runtime_put_sync(dev);
318         pm_runtime_disable(dev);
319
320         return 0;
321 }
322
323 /* instance-specific driver private data */
324 static const struct pruss_private_data am437x_pruss1_data = {
325         .has_no_sharedram = false,
326 };
327
328 static const struct pruss_private_data am437x_pruss0_data = {
329         .has_no_sharedram = true,
330 };
331
332 static const struct pruss_private_data am65x_j721e_pruss_data = {
333         .has_core_mux_clock = true,
334 };
335
336 static const struct of_device_id pruss_of_match[] = {
337         { .compatible = "ti,am3356-pruss" },
338         { .compatible = "ti,am4376-pruss0", .data = &am437x_pruss0_data, },
339         { .compatible = "ti,am4376-pruss1", .data = &am437x_pruss1_data, },
340         { .compatible = "ti,am5728-pruss" },
341         { .compatible = "ti,k2g-pruss" },
342         { .compatible = "ti,am654-icssg", .data = &am65x_j721e_pruss_data, },
343         { .compatible = "ti,j721e-icssg", .data = &am65x_j721e_pruss_data, },
344         { .compatible = "ti,am642-icssg", .data = &am65x_j721e_pruss_data, },
345         { .compatible = "ti,am625-pruss", .data = &am65x_j721e_pruss_data, },
346         {},
347 };
348 MODULE_DEVICE_TABLE(of, pruss_of_match);
349
350 static struct platform_driver pruss_driver = {
351         .driver = {
352                 .name = "pruss",
353                 .of_match_table = pruss_of_match,
354         },
355         .probe  = pruss_probe,
356         .remove = pruss_remove,
357 };
358 module_platform_driver(pruss_driver);
359
360 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
361 MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver");
362 MODULE_LICENSE("GPL v2");