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 * Tero Kristo <t-kristo@ti.com>
10 */
11
12 #include <linux/clk-provider.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/io.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/pruss_driver.h>
23 #include <linux/regmap.h>
24 #include <linux/remoteproc.h>
25 #include <linux/slab.h>
26 #include "pruss.h"
27
28 /**
29 * struct pruss_private_data - PRUSS driver private data
30 * @has_no_sharedram: flag to indicate the absence of PRUSS Shared Data RAM
31 * @has_core_mux_clock: flag to indicate the presence of PRUSS core clock
32 */
33 struct pruss_private_data {
34 bool has_no_sharedram;
35 bool has_core_mux_clock;
36 };
37
38 /**
39 * pruss_get() - get the pruss for a given PRU remoteproc
40 * @rproc: remoteproc handle of a PRU instance
41 *
42 * Finds the parent pruss device for a PRU given the @rproc handle of the
43 * PRU remote processor. This function increments the pruss device's refcount,
44 * so always use pruss_put() to decrement it back once pruss isn't needed
45 * anymore.
46 *
47 * This API doesn't check if @rproc is valid or not. It is expected the caller
48 * will have done a pru_rproc_get() on @rproc, before calling this API to make
49 * sure that @rproc is valid.
50 *
51 * Return: pruss handle on success, and an ERR_PTR on failure using one
52 * of the following error values
53 * -EINVAL if invalid parameter
54 * -ENODEV if PRU device or PRUSS device is not found
55 */
pruss_get(struct rproc * rproc)56 struct pruss *pruss_get(struct rproc *rproc)
57 {
58 struct pruss *pruss;
59 struct device *dev;
60 struct platform_device *ppdev;
61
62 if (IS_ERR_OR_NULL(rproc))
63 return ERR_PTR(-EINVAL);
64
65 dev = &rproc->dev;
66
67 /* make sure it is PRU rproc */
68 if (!dev->parent || !is_pru_rproc(dev->parent))
69 return ERR_PTR(-ENODEV);
70
71 ppdev = to_platform_device(dev->parent->parent);
72 pruss = platform_get_drvdata(ppdev);
73 if (!pruss)
74 return ERR_PTR(-ENODEV);
75
76 get_device(pruss->dev);
77
78 return pruss;
79 }
80 EXPORT_SYMBOL_GPL(pruss_get);
81
82 /**
83 * pruss_put() - decrement pruss device's usecount
84 * @pruss: pruss handle
85 *
86 * Complimentary function for pruss_get(). Needs to be called
87 * after the PRUSS is used, and only if the pruss_get() succeeds.
88 */
pruss_put(struct pruss * pruss)89 void pruss_put(struct pruss *pruss)
90 {
91 if (IS_ERR_OR_NULL(pruss))
92 return;
93
94 put_device(pruss->dev);
95 }
96 EXPORT_SYMBOL_GPL(pruss_put);
97
98 /**
99 * pruss_request_mem_region() - request a memory resource
100 * @pruss: the pruss instance
101 * @mem_id: the memory resource id
102 * @region: pointer to memory region structure to be filled in
103 *
104 * This function allows a client driver to request a memory resource,
105 * and if successful, will let the client driver own the particular
106 * memory region until released using the pruss_release_mem_region()
107 * API.
108 *
109 * Return: 0 if requested memory region is available (in such case pointer to
110 * memory region is returned via @region), an error otherwise
111 */
pruss_request_mem_region(struct pruss * pruss,enum pruss_mem mem_id,struct pruss_mem_region * region)112 int pruss_request_mem_region(struct pruss *pruss, enum pruss_mem mem_id,
113 struct pruss_mem_region *region)
114 {
115 if (!pruss || !region || mem_id >= PRUSS_MEM_MAX)
116 return -EINVAL;
117
118 mutex_lock(&pruss->lock);
119
120 if (pruss->mem_in_use[mem_id]) {
121 mutex_unlock(&pruss->lock);
122 return -EBUSY;
123 }
124
125 *region = pruss->mem_regions[mem_id];
126 pruss->mem_in_use[mem_id] = region;
127
128 mutex_unlock(&pruss->lock);
129
130 return 0;
131 }
132 EXPORT_SYMBOL_GPL(pruss_request_mem_region);
133
134 /**
135 * pruss_release_mem_region() - release a memory resource
136 * @pruss: the pruss instance
137 * @region: the memory region to release
138 *
139 * This function is the complimentary function to
140 * pruss_request_mem_region(), and allows the client drivers to
141 * release back a memory resource.
142 *
143 * Return: 0 on success, an error code otherwise
144 */
pruss_release_mem_region(struct pruss * pruss,struct pruss_mem_region * region)145 int pruss_release_mem_region(struct pruss *pruss,
146 struct pruss_mem_region *region)
147 {
148 int id;
149
150 if (!pruss || !region)
151 return -EINVAL;
152
153 mutex_lock(&pruss->lock);
154
155 /* find out the memory region being released */
156 for (id = 0; id < PRUSS_MEM_MAX; id++) {
157 if (pruss->mem_in_use[id] == region)
158 break;
159 }
160
161 if (id == PRUSS_MEM_MAX) {
162 mutex_unlock(&pruss->lock);
163 return -EINVAL;
164 }
165
166 pruss->mem_in_use[id] = NULL;
167
168 mutex_unlock(&pruss->lock);
169
170 return 0;
171 }
172 EXPORT_SYMBOL_GPL(pruss_release_mem_region);
173
174 /**
175 * pruss_cfg_get_gpmux() - get the current GPMUX value for a PRU device
176 * @pruss: pruss instance
177 * @pru_id: PRU identifier (0-1)
178 * @mux: pointer to store the current mux value into
179 *
180 * Return: 0 on success, or an error code otherwise
181 */
pruss_cfg_get_gpmux(struct pruss * pruss,enum pruss_pru_id pru_id,u8 * mux)182 int pruss_cfg_get_gpmux(struct pruss *pruss, enum pruss_pru_id pru_id, u8 *mux)
183 {
184 int ret;
185 u32 val;
186
187 if (pru_id >= PRUSS_NUM_PRUS || !mux)
188 return -EINVAL;
189
190 ret = pruss_cfg_read(pruss, PRUSS_CFG_GPCFG(pru_id), &val);
191 if (!ret)
192 *mux = (u8)((val & PRUSS_GPCFG_PRU_MUX_SEL_MASK) >>
193 PRUSS_GPCFG_PRU_MUX_SEL_SHIFT);
194 return ret;
195 }
196 EXPORT_SYMBOL_GPL(pruss_cfg_get_gpmux);
197
198 /**
199 * pruss_cfg_set_gpmux() - set the GPMUX value for a PRU device
200 * @pruss: pruss instance
201 * @pru_id: PRU identifier (0-1)
202 * @mux: new mux value for PRU
203 *
204 * Return: 0 on success, or an error code otherwise
205 */
pruss_cfg_set_gpmux(struct pruss * pruss,enum pruss_pru_id pru_id,u8 mux)206 int pruss_cfg_set_gpmux(struct pruss *pruss, enum pruss_pru_id pru_id, u8 mux)
207 {
208 if (mux >= PRUSS_GP_MUX_SEL_MAX ||
209 pru_id >= PRUSS_NUM_PRUS)
210 return -EINVAL;
211
212 return pruss_cfg_update(pruss, PRUSS_CFG_GPCFG(pru_id),
213 PRUSS_GPCFG_PRU_MUX_SEL_MASK,
214 (u32)mux << PRUSS_GPCFG_PRU_MUX_SEL_SHIFT);
215 }
216 EXPORT_SYMBOL_GPL(pruss_cfg_set_gpmux);
217
218 /**
219 * pruss_cfg_gpimode() - set the GPI mode of the PRU
220 * @pruss: the pruss instance handle
221 * @pru_id: id of the PRU core within the PRUSS
222 * @mode: GPI mode to set
223 *
224 * Sets the GPI mode for a given PRU by programming the
225 * corresponding PRUSS_CFG_GPCFGx register
226 *
227 * Return: 0 on success, or an error code otherwise
228 */
pruss_cfg_gpimode(struct pruss * pruss,enum pruss_pru_id pru_id,enum pruss_gpi_mode mode)229 int pruss_cfg_gpimode(struct pruss *pruss, enum pruss_pru_id pru_id,
230 enum pruss_gpi_mode mode)
231 {
232 if (pru_id >= PRUSS_NUM_PRUS || mode >= PRUSS_GPI_MODE_MAX)
233 return -EINVAL;
234
235 return pruss_cfg_update(pruss, PRUSS_CFG_GPCFG(pru_id),
236 PRUSS_GPCFG_PRU_GPI_MODE_MASK,
237 mode << PRUSS_GPCFG_PRU_GPI_MODE_SHIFT);
238 }
239 EXPORT_SYMBOL_GPL(pruss_cfg_gpimode);
240
241 /**
242 * pruss_cfg_miirt_enable() - Enable/disable MII RT Events
243 * @pruss: the pruss instance
244 * @enable: enable/disable
245 *
246 * Enable/disable the MII RT Events for the PRUSS.
247 *
248 * Return: 0 on success, or an error code otherwise
249 */
pruss_cfg_miirt_enable(struct pruss * pruss,bool enable)250 int pruss_cfg_miirt_enable(struct pruss *pruss, bool enable)
251 {
252 u32 set = enable ? PRUSS_MII_RT_EVENT_EN : 0;
253
254 return pruss_cfg_update(pruss, PRUSS_CFG_MII_RT,
255 PRUSS_MII_RT_EVENT_EN, set);
256 }
257 EXPORT_SYMBOL_GPL(pruss_cfg_miirt_enable);
258
259 /**
260 * pruss_cfg_xfr_enable() - Enable/disable XIN XOUT shift functionality
261 * @pruss: the pruss instance
262 * @pru_type: PRU core type identifier
263 * @enable: enable/disable
264 *
265 * Return: 0 on success, or an error code otherwise
266 */
pruss_cfg_xfr_enable(struct pruss * pruss,enum pru_type pru_type,bool enable)267 int pruss_cfg_xfr_enable(struct pruss *pruss, enum pru_type pru_type,
268 bool enable)
269 {
270 u32 mask, set;
271
272 switch (pru_type) {
273 case PRU_TYPE_PRU:
274 mask = PRUSS_SPP_XFER_SHIFT_EN;
275 break;
276 case PRU_TYPE_RTU:
277 mask = PRUSS_SPP_RTU_XFR_SHIFT_EN;
278 break;
279 default:
280 return -EINVAL;
281 }
282
283 set = enable ? mask : 0;
284
285 return pruss_cfg_update(pruss, PRUSS_CFG_SPP, mask, set);
286 }
287 EXPORT_SYMBOL_GPL(pruss_cfg_xfr_enable);
288
pruss_of_free_clk_provider(void * data)289 static void pruss_of_free_clk_provider(void *data)
290 {
291 struct device_node *clk_mux_np = data;
292
293 of_clk_del_provider(clk_mux_np);
294 of_node_put(clk_mux_np);
295 }
296
pruss_clk_unregister_mux(void * data)297 static void pruss_clk_unregister_mux(void *data)
298 {
299 clk_unregister_mux(data);
300 }
301
pruss_clk_mux_setup(struct pruss * pruss,struct clk * clk_mux,char * mux_name,struct device_node * clks_np)302 static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux,
303 char *mux_name, struct device_node *clks_np)
304 {
305 struct device_node *clk_mux_np;
306 struct device *dev = pruss->dev;
307 char *clk_mux_name;
308 unsigned int num_parents;
309 const char **parent_names;
310 void __iomem *reg;
311 u32 reg_offset;
312 int ret;
313
314 clk_mux_np = of_get_child_by_name(clks_np, mux_name);
315 if (!clk_mux_np) {
316 dev_err(dev, "%pOF is missing its '%s' node\n", clks_np,
317 mux_name);
318 return -ENODEV;
319 }
320
321 num_parents = of_clk_get_parent_count(clk_mux_np);
322 if (num_parents < 1) {
323 dev_err(dev, "mux-clock %pOF must have parents\n", clk_mux_np);
324 ret = -EINVAL;
325 goto put_clk_mux_np;
326 }
327
328 parent_names = devm_kcalloc(dev, sizeof(*parent_names), num_parents,
329 GFP_KERNEL);
330 if (!parent_names) {
331 ret = -ENOMEM;
332 goto put_clk_mux_np;
333 }
334
335 of_clk_parent_fill(clk_mux_np, parent_names, num_parents);
336
337 clk_mux_name = devm_kasprintf(dev, GFP_KERNEL, "%s.%pOFn",
338 dev_name(dev), clk_mux_np);
339 if (!clk_mux_name) {
340 ret = -ENOMEM;
341 goto put_clk_mux_np;
342 }
343
344 ret = of_property_read_u32(clk_mux_np, "reg", ®_offset);
345 if (ret)
346 goto put_clk_mux_np;
347
348 reg = pruss->cfg_base + reg_offset;
349
350 clk_mux = clk_register_mux(NULL, clk_mux_name, parent_names,
351 num_parents, 0, reg, 0, 1, 0, NULL);
352 if (IS_ERR(clk_mux)) {
353 ret = PTR_ERR(clk_mux);
354 goto put_clk_mux_np;
355 }
356
357 ret = devm_add_action_or_reset(dev, pruss_clk_unregister_mux, clk_mux);
358 if (ret) {
359 dev_err(dev, "failed to add clkmux unregister action %d", ret);
360 goto put_clk_mux_np;
361 }
362
363 ret = of_clk_add_provider(clk_mux_np, of_clk_src_simple_get, clk_mux);
364 if (ret)
365 goto put_clk_mux_np;
366
367 ret = devm_add_action_or_reset(dev, pruss_of_free_clk_provider,
368 clk_mux_np);
369 if (ret) {
370 dev_err(dev, "failed to add clkmux free action %d", ret);
371 goto put_clk_mux_np;
372 }
373
374 return 0;
375
376 put_clk_mux_np:
377 of_node_put(clk_mux_np);
378 return ret;
379 }
380
pruss_clk_init(struct pruss * pruss,struct device_node * cfg_node)381 static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
382 {
383 struct device *dev = pruss->dev;
384 struct device_node *clks_np __free(device_node) =
385 of_get_child_by_name(cfg_node, "clocks");
386 const struct pruss_private_data *data = of_device_get_match_data(dev);
387 int ret;
388
389 if (!clks_np)
390 return dev_err_probe(dev, -ENODEV,
391 "%pOF is missing its 'clocks' node\n",
392 cfg_node);
393
394 if (data && data->has_core_mux_clock) {
395 ret = pruss_clk_mux_setup(pruss, pruss->core_clk_mux,
396 "coreclk-mux", clks_np);
397 if (ret)
398 return dev_err_probe(dev, ret,
399 "failed to setup coreclk-mux\n");
400 }
401
402 ret = pruss_clk_mux_setup(pruss, pruss->iep_clk_mux, "iepclk-mux",
403 clks_np);
404 if (ret)
405 return dev_err_probe(dev, ret, "failed to setup iepclk-mux\n");
406
407 return 0;
408 }
409
pruss_of_setup_memories(struct device * dev,struct pruss * pruss)410 static int pruss_of_setup_memories(struct device *dev, struct pruss *pruss)
411 {
412 struct device_node *np = dev_of_node(dev);
413 struct device_node *child __free(device_node) =
414 of_get_child_by_name(np, "memories");
415 const struct pruss_private_data *data = of_device_get_match_data(dev);
416 const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" };
417 int i;
418
419 if (!child)
420 return dev_err_probe(dev, -ENODEV,
421 "%pOF is missing its 'memories' node\n",
422 child);
423
424 for (i = 0; i < PRUSS_MEM_MAX; i++) {
425 struct resource res;
426 int index;
427
428 /*
429 * On AM437x one of two PRUSS units don't contain Shared RAM,
430 * skip it
431 */
432 if (data && data->has_no_sharedram && i == PRUSS_MEM_SHRD_RAM2)
433 continue;
434
435 index = of_property_match_string(child, "reg-names",
436 mem_names[i]);
437 if (index < 0)
438 return index;
439
440 if (of_address_to_resource(child, index, &res))
441 return -EINVAL;
442
443 pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
444 resource_size(&res));
445 if (!pruss->mem_regions[i].va)
446 return dev_err_probe(dev, -ENOMEM,
447 "failed to parse and map memory resource %d %s\n",
448 i, mem_names[i]);
449 pruss->mem_regions[i].pa = res.start;
450 pruss->mem_regions[i].size = resource_size(&res);
451
452 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
453 mem_names[i], &pruss->mem_regions[i].pa,
454 pruss->mem_regions[i].size, pruss->mem_regions[i].va);
455 }
456
457 return 0;
458 }
459
460 static struct regmap_config regmap_conf = {
461 .reg_bits = 32,
462 .val_bits = 32,
463 .reg_stride = 4,
464 };
465
pruss_cfg_of_init(struct device * dev,struct pruss * pruss)466 static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
467 {
468 struct device_node *np = dev_of_node(dev);
469 struct device_node *child __free(device_node) =
470 of_get_child_by_name(np, "cfg");
471 struct resource res;
472 int ret;
473
474 if (!child)
475 return dev_err_probe(dev, -ENODEV,
476 "%pOF is missing its 'cfg' node\n", child);
477
478 if (of_address_to_resource(child, 0, &res))
479 return -ENOMEM;
480
481 pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
482 if (!pruss->cfg_base)
483 return -ENOMEM;
484
485 regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
486 (u64)res.start);
487 regmap_conf.max_register = resource_size(&res) - 4;
488
489 pruss->cfg_regmap = devm_regmap_init_mmio(dev, pruss->cfg_base,
490 ®map_conf);
491 kfree(regmap_conf.name);
492 if (IS_ERR(pruss->cfg_regmap))
493 return dev_err_probe(dev, PTR_ERR(pruss->cfg_regmap),
494 "regmap_init_mmio failed for cfg\n");
495
496 ret = pruss_clk_init(pruss, child);
497 if (ret)
498 return dev_err_probe(dev, ret, "pruss_clk_init failed\n");
499
500 return 0;
501 }
502
pruss_probe(struct platform_device * pdev)503 static int pruss_probe(struct platform_device *pdev)
504 {
505 struct device *dev = &pdev->dev;
506 struct pruss *pruss;
507 int ret;
508
509 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
510 if (ret) {
511 dev_err(dev, "failed to set the DMA coherent mask");
512 return ret;
513 }
514
515 pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL);
516 if (!pruss)
517 return -ENOMEM;
518
519 pruss->dev = dev;
520 mutex_init(&pruss->lock);
521
522 ret = pruss_of_setup_memories(dev, pruss);
523 if (ret < 0)
524 return ret;
525
526 platform_set_drvdata(pdev, pruss);
527
528 pm_runtime_enable(dev);
529 ret = pm_runtime_resume_and_get(dev);
530 if (ret < 0) {
531 dev_err(dev, "couldn't enable module\n");
532 goto rpm_disable;
533 }
534
535 ret = pruss_cfg_of_init(dev, pruss);
536 if (ret < 0)
537 goto rpm_put;
538
539 ret = devm_of_platform_populate(dev);
540 if (ret) {
541 dev_err(dev, "failed to register child devices\n");
542 goto rpm_put;
543 }
544
545 return 0;
546
547 rpm_put:
548 pm_runtime_put_sync(dev);
549 rpm_disable:
550 pm_runtime_disable(dev);
551 return ret;
552 }
553
pruss_remove(struct platform_device * pdev)554 static void pruss_remove(struct platform_device *pdev)
555 {
556 struct device *dev = &pdev->dev;
557
558 devm_of_platform_depopulate(dev);
559
560 pm_runtime_put_sync(dev);
561 pm_runtime_disable(dev);
562 }
563
564 /* instance-specific driver private data */
565 static const struct pruss_private_data am437x_pruss1_data = {
566 .has_no_sharedram = false,
567 };
568
569 static const struct pruss_private_data am437x_pruss0_data = {
570 .has_no_sharedram = true,
571 };
572
573 static const struct pruss_private_data am65x_j721e_pruss_data = {
574 .has_core_mux_clock = true,
575 };
576
577 static const struct of_device_id pruss_of_match[] = {
578 { .compatible = "ti,am3356-pruss" },
579 { .compatible = "ti,am4376-pruss0", .data = &am437x_pruss0_data, },
580 { .compatible = "ti,am4376-pruss1", .data = &am437x_pruss1_data, },
581 { .compatible = "ti,am5728-pruss" },
582 { .compatible = "ti,k2g-pruss" },
583 { .compatible = "ti,am654-icssg", .data = &am65x_j721e_pruss_data, },
584 { .compatible = "ti,j721e-icssg", .data = &am65x_j721e_pruss_data, },
585 { .compatible = "ti,am642-icssg", .data = &am65x_j721e_pruss_data, },
586 { .compatible = "ti,am625-pruss", .data = &am65x_j721e_pruss_data, },
587 {},
588 };
589 MODULE_DEVICE_TABLE(of, pruss_of_match);
590
591 static struct platform_driver pruss_driver = {
592 .driver = {
593 .name = "pruss",
594 .of_match_table = pruss_of_match,
595 },
596 .probe = pruss_probe,
597 .remove_new = pruss_remove,
598 };
599 module_platform_driver(pruss_driver);
600
601 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
602 MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver");
603 MODULE_LICENSE("GPL v2");
604