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 { .family = "AM64X", .data = &am64_ep_map }, 24 { .family = "J721S2", .data = &j721s2_ep_map }, 25 { /* sentinel */ } 26 }; 27 28 struct psil_endpoint_config *psil_get_ep_config(u32 thread_id) 29 { 30 int i; 31 32 mutex_lock(&ep_map_mutex); 33 if (!soc_ep_map) { 34 const struct soc_device_attribute *soc; 35 36 soc = soc_device_match(k3_soc_devices); 37 if (soc) { 38 soc_ep_map = soc->data; 39 } else { 40 pr_err("PSIL: No compatible machine found for map\n"); 41 mutex_unlock(&ep_map_mutex); 42 return ERR_PTR(-ENOTSUPP); 43 } 44 pr_debug("%s: Using map for %s\n", __func__, soc_ep_map->name); 45 } 46 mutex_unlock(&ep_map_mutex); 47 48 if (thread_id & K3_PSIL_DST_THREAD_ID_OFFSET && soc_ep_map->dst) { 49 /* check in destination thread map */ 50 for (i = 0; i < soc_ep_map->dst_count; i++) { 51 if (soc_ep_map->dst[i].thread_id == thread_id) 52 return &soc_ep_map->dst[i].ep_config; 53 } 54 } 55 56 thread_id &= ~K3_PSIL_DST_THREAD_ID_OFFSET; 57 if (soc_ep_map->src) { 58 for (i = 0; i < soc_ep_map->src_count; i++) { 59 if (soc_ep_map->src[i].thread_id == thread_id) 60 return &soc_ep_map->src[i].ep_config; 61 } 62 } 63 64 return ERR_PTR(-ENOENT); 65 } 66 EXPORT_SYMBOL_GPL(psil_get_ep_config); 67 68 int psil_set_new_ep_config(struct device *dev, const char *name, 69 struct psil_endpoint_config *ep_config) 70 { 71 struct psil_endpoint_config *dst_ep_config; 72 struct of_phandle_args dma_spec; 73 u32 thread_id; 74 int index; 75 76 if (!dev || !dev->of_node) 77 return -EINVAL; 78 79 index = of_property_match_string(dev->of_node, "dma-names", name); 80 if (index < 0) 81 return index; 82 83 if (of_parse_phandle_with_args(dev->of_node, "dmas", "#dma-cells", 84 index, &dma_spec)) 85 return -ENOENT; 86 87 thread_id = dma_spec.args[0]; 88 89 dst_ep_config = psil_get_ep_config(thread_id); 90 if (IS_ERR(dst_ep_config)) { 91 pr_err("PSIL: thread ID 0x%04x not defined in map\n", 92 thread_id); 93 of_node_put(dma_spec.np); 94 return PTR_ERR(dst_ep_config); 95 } 96 97 memcpy(dst_ep_config, ep_config, sizeof(*dst_ep_config)); 98 99 of_node_put(dma_spec.np); 100 return 0; 101 } 102 EXPORT_SYMBOL_GPL(psil_set_new_ep_config); 103