xref: /openbmc/linux/drivers/soc/ti/pruss.c (revision 3ddc8b84)
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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 
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 
297 static void pruss_clk_unregister_mux(void *data)
298 {
299 	clk_unregister_mux(data);
300 }
301 
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", &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 
381 static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
382 {
383 	const struct pruss_private_data *data;
384 	struct device_node *clks_np;
385 	struct device *dev = pruss->dev;
386 	int ret = 0;
387 
388 	data = of_device_get_match_data(dev);
389 
390 	clks_np = of_get_child_by_name(cfg_node, "clocks");
391 	if (!clks_np) {
392 		dev_err(dev, "%pOF is missing its 'clocks' node\n", cfg_node);
393 		return -ENODEV;
394 	}
395 
396 	if (data && data->has_core_mux_clock) {
397 		ret = pruss_clk_mux_setup(pruss, pruss->core_clk_mux,
398 					  "coreclk-mux", clks_np);
399 		if (ret) {
400 			dev_err(dev, "failed to setup coreclk-mux\n");
401 			goto put_clks_node;
402 		}
403 	}
404 
405 	ret = pruss_clk_mux_setup(pruss, pruss->iep_clk_mux, "iepclk-mux",
406 				  clks_np);
407 	if (ret) {
408 		dev_err(dev, "failed to setup iepclk-mux\n");
409 		goto put_clks_node;
410 	}
411 
412 put_clks_node:
413 	of_node_put(clks_np);
414 
415 	return ret;
416 }
417 
418 static struct regmap_config regmap_conf = {
419 	.reg_bits = 32,
420 	.val_bits = 32,
421 	.reg_stride = 4,
422 };
423 
424 static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
425 {
426 	struct device_node *np = dev_of_node(dev);
427 	struct device_node *child;
428 	struct resource res;
429 	int ret;
430 
431 	child = of_get_child_by_name(np, "cfg");
432 	if (!child) {
433 		dev_err(dev, "%pOF is missing its 'cfg' node\n", child);
434 		return -ENODEV;
435 	}
436 
437 	if (of_address_to_resource(child, 0, &res)) {
438 		ret = -ENOMEM;
439 		goto node_put;
440 	}
441 
442 	pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
443 	if (!pruss->cfg_base) {
444 		ret = -ENOMEM;
445 		goto node_put;
446 	}
447 
448 	regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
449 				     (u64)res.start);
450 	regmap_conf.max_register = resource_size(&res) - 4;
451 
452 	pruss->cfg_regmap = devm_regmap_init_mmio(dev, pruss->cfg_base,
453 						  &regmap_conf);
454 	kfree(regmap_conf.name);
455 	if (IS_ERR(pruss->cfg_regmap)) {
456 		dev_err(dev, "regmap_init_mmio failed for cfg, ret = %ld\n",
457 			PTR_ERR(pruss->cfg_regmap));
458 		ret = PTR_ERR(pruss->cfg_regmap);
459 		goto node_put;
460 	}
461 
462 	ret = pruss_clk_init(pruss, child);
463 	if (ret)
464 		dev_err(dev, "pruss_clk_init failed, ret = %d\n", ret);
465 
466 node_put:
467 	of_node_put(child);
468 	return ret;
469 }
470 
471 static int pruss_probe(struct platform_device *pdev)
472 {
473 	struct device *dev = &pdev->dev;
474 	struct device_node *np = dev_of_node(dev);
475 	struct device_node *child;
476 	struct pruss *pruss;
477 	struct resource res;
478 	int ret, i, index;
479 	const struct pruss_private_data *data;
480 	const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" };
481 
482 	data = of_device_get_match_data(&pdev->dev);
483 
484 	ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
485 	if (ret) {
486 		dev_err(dev, "failed to set the DMA coherent mask");
487 		return ret;
488 	}
489 
490 	pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL);
491 	if (!pruss)
492 		return -ENOMEM;
493 
494 	pruss->dev = dev;
495 	mutex_init(&pruss->lock);
496 
497 	child = of_get_child_by_name(np, "memories");
498 	if (!child) {
499 		dev_err(dev, "%pOF is missing its 'memories' node\n", child);
500 		return -ENODEV;
501 	}
502 
503 	for (i = 0; i < PRUSS_MEM_MAX; i++) {
504 		/*
505 		 * On AM437x one of two PRUSS units don't contain Shared RAM,
506 		 * skip it
507 		 */
508 		if (data && data->has_no_sharedram && i == PRUSS_MEM_SHRD_RAM2)
509 			continue;
510 
511 		index = of_property_match_string(child, "reg-names",
512 						 mem_names[i]);
513 		if (index < 0) {
514 			of_node_put(child);
515 			return index;
516 		}
517 
518 		if (of_address_to_resource(child, index, &res)) {
519 			of_node_put(child);
520 			return -EINVAL;
521 		}
522 
523 		pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
524 							resource_size(&res));
525 		if (!pruss->mem_regions[i].va) {
526 			dev_err(dev, "failed to parse and map memory resource %d %s\n",
527 				i, mem_names[i]);
528 			of_node_put(child);
529 			return -ENOMEM;
530 		}
531 		pruss->mem_regions[i].pa = res.start;
532 		pruss->mem_regions[i].size = resource_size(&res);
533 
534 		dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
535 			mem_names[i], &pruss->mem_regions[i].pa,
536 			pruss->mem_regions[i].size, pruss->mem_regions[i].va);
537 	}
538 	of_node_put(child);
539 
540 	platform_set_drvdata(pdev, pruss);
541 
542 	pm_runtime_enable(dev);
543 	ret = pm_runtime_resume_and_get(dev);
544 	if (ret < 0) {
545 		dev_err(dev, "couldn't enable module\n");
546 		goto rpm_disable;
547 	}
548 
549 	ret = pruss_cfg_of_init(dev, pruss);
550 	if (ret < 0)
551 		goto rpm_put;
552 
553 	ret = devm_of_platform_populate(dev);
554 	if (ret) {
555 		dev_err(dev, "failed to register child devices\n");
556 		goto rpm_put;
557 	}
558 
559 	return 0;
560 
561 rpm_put:
562 	pm_runtime_put_sync(dev);
563 rpm_disable:
564 	pm_runtime_disable(dev);
565 	return ret;
566 }
567 
568 static int pruss_remove(struct platform_device *pdev)
569 {
570 	struct device *dev = &pdev->dev;
571 
572 	devm_of_platform_depopulate(dev);
573 
574 	pm_runtime_put_sync(dev);
575 	pm_runtime_disable(dev);
576 
577 	return 0;
578 }
579 
580 /* instance-specific driver private data */
581 static const struct pruss_private_data am437x_pruss1_data = {
582 	.has_no_sharedram = false,
583 };
584 
585 static const struct pruss_private_data am437x_pruss0_data = {
586 	.has_no_sharedram = true,
587 };
588 
589 static const struct pruss_private_data am65x_j721e_pruss_data = {
590 	.has_core_mux_clock = true,
591 };
592 
593 static const struct of_device_id pruss_of_match[] = {
594 	{ .compatible = "ti,am3356-pruss" },
595 	{ .compatible = "ti,am4376-pruss0", .data = &am437x_pruss0_data, },
596 	{ .compatible = "ti,am4376-pruss1", .data = &am437x_pruss1_data, },
597 	{ .compatible = "ti,am5728-pruss" },
598 	{ .compatible = "ti,k2g-pruss" },
599 	{ .compatible = "ti,am654-icssg", .data = &am65x_j721e_pruss_data, },
600 	{ .compatible = "ti,j721e-icssg", .data = &am65x_j721e_pruss_data, },
601 	{ .compatible = "ti,am642-icssg", .data = &am65x_j721e_pruss_data, },
602 	{ .compatible = "ti,am625-pruss", .data = &am65x_j721e_pruss_data, },
603 	{},
604 };
605 MODULE_DEVICE_TABLE(of, pruss_of_match);
606 
607 static struct platform_driver pruss_driver = {
608 	.driver = {
609 		.name = "pruss",
610 		.of_match_table = pruss_of_match,
611 	},
612 	.probe  = pruss_probe,
613 	.remove = pruss_remove,
614 };
615 module_platform_driver(pruss_driver);
616 
617 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
618 MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver");
619 MODULE_LICENSE("GPL v2");
620