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