1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "ipc_icmsg_v1.h"
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/device.h>
11 #include "icmsg_v1.h"
12 
13 #include <zephyr/ipc/ipc_service_backend.h>
14 
15 #define DT_DRV_COMPAT	zephyr_ipc_icmsg
16 
register_ept(const struct device * instance,void ** token,const struct ipc_ept_cfg * cfg)17 static int register_ept(const struct device *instance, void **token,
18 			const struct ipc_ept_cfg *cfg)
19 {
20 	const struct icmsg_config_t *conf = instance->config;
21 	struct icmsg_data_t *dev_data = instance->data;
22 
23 	/* Only one endpoint is supported. No need for a token. */
24 	*token = NULL;
25 
26 	return icmsg_open(conf, dev_data, &cfg->cb, cfg->priv);
27 }
28 
deregister_ept(const struct device * instance,void * token)29 static int deregister_ept(const struct device *instance, void *token)
30 {
31 	const struct icmsg_config_t *conf = instance->config;
32 	struct icmsg_data_t *dev_data = instance->data;
33 
34 	return icmsg_close(conf, dev_data);
35 }
36 
send(const struct device * instance,void * token,const void * msg,size_t len)37 static int send(const struct device *instance, void *token,
38 		const void *msg, size_t len)
39 {
40 	const struct icmsg_config_t *conf = instance->config;
41 	struct icmsg_data_t *dev_data = instance->data;
42 
43 	return icmsg_send(conf, dev_data, msg, len);
44 }
45 
46 const static struct ipc_service_backend backend_ops = {
47 	.register_endpoint = register_ept,
48 	.deregister_endpoint = deregister_ept,
49 	.send = send,
50 };
51 
backend_init(const struct device * instance)52 static int backend_init(const struct device *instance)
53 {
54 	return 0;
55 }
56 
57 #define DEFINE_BACKEND_DEVICE(i)					\
58 	static const struct icmsg_config_t backend_config_##i = {	\
59 		.mbox_tx = MBOX_DT_SPEC_INST_GET(i, tx),		\
60 		.mbox_rx = MBOX_DT_SPEC_INST_GET(i, rx),		\
61 	};								\
62 									\
63 	PBUF_DEFINE(tx_pb_##i,						\
64 			DT_REG_ADDR(DT_INST_PHANDLE(i, tx_region)),	\
65 			DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)),	\
66 			DT_INST_PROP_OR(i, dcache_alignment, 0));	\
67 	PBUF_DEFINE(rx_pb_##i,						\
68 			DT_REG_ADDR(DT_INST_PHANDLE(i, rx_region)),	\
69 			DT_REG_SIZE(DT_INST_PHANDLE(i, rx_region)),	\
70 			DT_INST_PROP_OR(i, dcache_alignment, 0));	\
71 									\
72 	static struct icmsg_data_t backend_data_##i = {			\
73 		.tx_pb = &tx_pb_##i,					\
74 		.rx_pb = &rx_pb_##i,					\
75 	};								\
76 									\
77 	DEVICE_DT_INST_DEFINE(i,					\
78 			 &backend_init,					\
79 			 NULL,						\
80 			 &backend_data_##i,				\
81 			 &backend_config_##i,				\
82 			 POST_KERNEL,					\
83 			 CONFIG_IPC_SERVICE_REG_BACKEND_PRIORITY,	\
84 			 &backend_ops);
85 
86 DT_INST_FOREACH_STATUS_OKAY(DEFINE_BACKEND_DEVICE)
87