xref: /openbmc/linux/drivers/dma/ti/k3-psil.c (revision 6c33a6f4)
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 
13 #include "k3-psil-priv.h"
14 
15 static DEFINE_MUTEX(ep_map_mutex);
16 static struct psil_ep_map *soc_ep_map;
17 
18 struct psil_endpoint_config *psil_get_ep_config(u32 thread_id)
19 {
20 	int i;
21 
22 	mutex_lock(&ep_map_mutex);
23 	if (!soc_ep_map) {
24 		if (of_machine_is_compatible("ti,am654")) {
25 			soc_ep_map = &am654_ep_map;
26 		} else if (of_machine_is_compatible("ti,j721e")) {
27 			soc_ep_map = &j721e_ep_map;
28 		} else {
29 			pr_err("PSIL: No compatible machine found for map\n");
30 			return ERR_PTR(-ENOTSUPP);
31 		}
32 		pr_debug("%s: Using map for %s\n", __func__, soc_ep_map->name);
33 	}
34 	mutex_unlock(&ep_map_mutex);
35 
36 	if (thread_id & K3_PSIL_DST_THREAD_ID_OFFSET && soc_ep_map->dst) {
37 		/* check in destination thread map */
38 		for (i = 0; i < soc_ep_map->dst_count; i++) {
39 			if (soc_ep_map->dst[i].thread_id == thread_id)
40 				return &soc_ep_map->dst[i].ep_config;
41 		}
42 	}
43 
44 	thread_id &= ~K3_PSIL_DST_THREAD_ID_OFFSET;
45 	if (soc_ep_map->src) {
46 		for (i = 0; i < soc_ep_map->src_count; i++) {
47 			if (soc_ep_map->src[i].thread_id == thread_id)
48 				return &soc_ep_map->src[i].ep_config;
49 		}
50 	}
51 
52 	return ERR_PTR(-ENOENT);
53 }
54 EXPORT_SYMBOL_GPL(psil_get_ep_config);
55 
56 int psil_set_new_ep_config(struct device *dev, const char *name,
57 			   struct psil_endpoint_config *ep_config)
58 {
59 	struct psil_endpoint_config *dst_ep_config;
60 	struct of_phandle_args dma_spec;
61 	u32 thread_id;
62 	int index;
63 
64 	if (!dev || !dev->of_node)
65 		return -EINVAL;
66 
67 	index = of_property_match_string(dev->of_node, "dma-names", name);
68 	if (index < 0)
69 		return index;
70 
71 	if (of_parse_phandle_with_args(dev->of_node, "dmas", "#dma-cells",
72 				       index, &dma_spec))
73 		return -ENOENT;
74 
75 	thread_id = dma_spec.args[0];
76 
77 	dst_ep_config = psil_get_ep_config(thread_id);
78 	if (IS_ERR(dst_ep_config)) {
79 		pr_err("PSIL: thread ID 0x%04x not defined in map\n",
80 		       thread_id);
81 		of_node_put(dma_spec.np);
82 		return PTR_ERR(dst_ep_config);
83 	}
84 
85 	memcpy(dst_ep_config, ep_config, sizeof(*dst_ep_config));
86 
87 	of_node_put(dma_spec.np);
88 	return 0;
89 }
90 EXPORT_SYMBOL_GPL(psil_set_new_ep_config);
91