1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com
4  *  Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/device.h>
9 #include <linux/init.h>
10 #include <linux/mutex.h>
11 #include <linux/of.h>
12 #include <linux/sys_soc.h>
13 
14 #include "k3-psil-priv.h"
15 
16 static DEFINE_MUTEX(ep_map_mutex);
17 static const struct psil_ep_map *soc_ep_map;
18 
19 static const struct soc_device_attribute k3_soc_devices[] = {
20 	{ .family = "AM65X", .data = &am654_ep_map },
21 	{ .family = "J721E", .data = &j721e_ep_map },
22 	{ .family = "J7200", .data = &j7200_ep_map },
23 	{ /* sentinel */ }
24 };
25 
psil_get_ep_config(u32 thread_id)26 struct psil_endpoint_config *psil_get_ep_config(u32 thread_id)
27 {
28 	int i;
29 
30 	mutex_lock(&ep_map_mutex);
31 	if (!soc_ep_map) {
32 		const struct soc_device_attribute *soc;
33 
34 		soc = soc_device_match(k3_soc_devices);
35 		if (soc) {
36 			soc_ep_map = soc->data;
37 		} else {
38 			pr_err("PSIL: No compatible machine found for map\n");
39 			mutex_unlock(&ep_map_mutex);
40 			return ERR_PTR(-ENOTSUPP);
41 		}
42 		pr_debug("%s: Using map for %s\n", __func__, soc_ep_map->name);
43 	}
44 	mutex_unlock(&ep_map_mutex);
45 
46 	if (thread_id & K3_PSIL_DST_THREAD_ID_OFFSET && soc_ep_map->dst) {
47 		/* check in destination thread map */
48 		for (i = 0; i < soc_ep_map->dst_count; i++) {
49 			if (soc_ep_map->dst[i].thread_id == thread_id)
50 				return &soc_ep_map->dst[i].ep_config;
51 		}
52 	}
53 
54 	thread_id &= ~K3_PSIL_DST_THREAD_ID_OFFSET;
55 	if (soc_ep_map->src) {
56 		for (i = 0; i < soc_ep_map->src_count; i++) {
57 			if (soc_ep_map->src[i].thread_id == thread_id)
58 				return &soc_ep_map->src[i].ep_config;
59 		}
60 	}
61 
62 	return ERR_PTR(-ENOENT);
63 }
64 EXPORT_SYMBOL_GPL(psil_get_ep_config);
65 
psil_set_new_ep_config(struct device * dev,const char * name,struct psil_endpoint_config * ep_config)66 int psil_set_new_ep_config(struct device *dev, const char *name,
67 			   struct psil_endpoint_config *ep_config)
68 {
69 	struct psil_endpoint_config *dst_ep_config;
70 	struct of_phandle_args dma_spec;
71 	u32 thread_id;
72 	int index;
73 
74 	if (!dev || !dev->of_node)
75 		return -EINVAL;
76 
77 	index = of_property_match_string(dev->of_node, "dma-names", name);
78 	if (index < 0)
79 		return index;
80 
81 	if (of_parse_phandle_with_args(dev->of_node, "dmas", "#dma-cells",
82 				       index, &dma_spec))
83 		return -ENOENT;
84 
85 	thread_id = dma_spec.args[0];
86 
87 	dst_ep_config = psil_get_ep_config(thread_id);
88 	if (IS_ERR(dst_ep_config)) {
89 		pr_err("PSIL: thread ID 0x%04x not defined in map\n",
90 		       thread_id);
91 		of_node_put(dma_spec.np);
92 		return PTR_ERR(dst_ep_config);
93 	}
94 
95 	memcpy(dst_ep_config, ep_config, sizeof(*dst_ep_config));
96 
97 	of_node_put(dma_spec.np);
98 	return 0;
99 }
100 EXPORT_SYMBOL_GPL(psil_set_new_ep_config);
101