1 /*
2  * Copyright (c) 2020, Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT intel_adsp_mailbox
8 
9 #include <device.h>
10 #include <soc.h>
11 #include <drivers/ipm.h>
12 
13 #include <platform/mailbox.h>
14 #include <platform/shim.h>
15 
16 #include <logging/log.h>
17 LOG_MODULE_REGISTER(ipm_adsp, CONFIG_IPM_LOG_LEVEL);
18 
19 /*
20  * With IPM data might be transferred by using ID field for simple
21  * messages or via shared memory. Following parameters specify maximum
22  * values for ID and DATA.
23  */
24 #define IPM_INTEL_ADSP_MAX_DATA_SIZE		256
25 #define IPM_INTEL_ADSP_MAX_ID_VAL		IPC_DIPCI_MSG_MASK
26 
27 /* Mailbox ADSP -> Host */
28 #define IPM_INTEL_ADSP_MAILBOX_OUT		MAILBOX_DSPBOX_BASE
29 #define IPM_INTEL_ADSP_MAILBOX_OUT_SIZE	MAILBOX_DSPBOX_SIZE
30 
31 BUILD_ASSERT(IPM_INTEL_ADSP_MAILBOX_OUT_SIZE >= IPM_INTEL_ADSP_MAX_DATA_SIZE);
32 
33 /* Mailbox Host -> ADSP */
34 #define IPM_INTEL_ADSP_MAILBOX_IN		MAILBOX_HOSTBOX_BASE
35 #define IPM_INTEL_ADSP_MAILBOX_IN_SIZE	MAILBOX_HOSTBOX_SIZE
36 
37 BUILD_ASSERT(IPM_INTEL_ADSP_MAILBOX_IN_SIZE >= IPM_INTEL_ADSP_MAX_DATA_SIZE);
38 
39 struct ipm_adsp_config {
40 	void (*irq_config_func)(const struct device *dev);
41 };
42 
43 struct ipm_adsp_data {
44 	ipm_callback_t callback;
45 	void *user_data;
46 };
47 
ipm_adsp_isr(const struct device * dev)48 static void ipm_adsp_isr(const struct device *dev)
49 {
50 	const struct ipm_adsp_data *data = dev->data;
51 	uint32_t dipcctl, dipcie, dipct;
52 
53 	dipct = ipc_read(IPC_DIPCT);
54 	dipcie = ipc_read(IPC_DIPCIE);
55 	dipcctl = ipc_read(IPC_DIPCCTL);
56 
57 	LOG_DBG("dipct 0x%x dipcie 0x%x dipcctl 0x%x", dipct, dipcie, dipcctl);
58 
59 	/*
60 	 * DSP core has received a message from IPC initiator (HOST).
61 	 * Initiator set Doorbel mechanism (HIPCI_BUSY bit).
62 	 */
63 	if (dipct & IPC_DIPCT_BUSY && dipcctl & IPC_DIPCCTL_IPCTBIE) {
64 		/* Mask BUSY interrupt */
65 		ipc_write(IPC_DIPCCTL, dipcctl & ~IPC_DIPCCTL_IPCTBIE);
66 
67 		if (data->callback) {
68 			SOC_DCACHE_INVALIDATE((void *)IPM_INTEL_ADSP_MAILBOX_IN,
69 					      IPM_INTEL_ADSP_MAILBOX_IN_SIZE);
70 			/* Use zero copy */
71 			data->callback(dev, data->user_data,
72 				       dipct & IPC_DIPCI_MSG_MASK,
73 				       (void *)IPM_INTEL_ADSP_MAILBOX_IN);
74 		}
75 
76 		/*
77 		 * Clear BUSY indicating to the Host that the message is
78 		 * received, and DSP is ready to accept another message
79 		 */
80 		ipc_write(IPC_DIPCT, ipc_read(IPC_DIPCT) | IPC_DIPCT_BUSY);
81 
82 		/* Unmask BUSY interrupts */
83 		ipc_write(IPC_DIPCCTL,
84 			  ipc_read(IPC_DIPCCTL) | IPC_DIPCCTL_IPCTBIE);
85 	}
86 
87 	/*
88 	 * DSP Initiator DONE indicates that we got reply from HOST that message
89 	 * is received and we can send another message.
90 	 */
91 	if (dipcie & IPC_DIPCIE_DONE && dipcctl & IPC_DIPCCTL_IPCIDIE) {
92 		/* Mask DONE interrupt */
93 		ipc_write(IPC_DIPCCTL,
94 			  ipc_read(IPC_DIPCCTL) & ~IPC_DIPCCTL_IPCIDIE);
95 
96 		/* Clear DONE bit, Notify HOST that operation is completed */
97 		ipc_write(IPC_DIPCIE,
98 			  ipc_read(IPC_DIPCIE) | IPC_DIPCIE_DONE);
99 
100 		/* Unmask DONE interrupt */
101 		ipc_write(IPC_DIPCCTL,
102 			  ipc_read(IPC_DIPCCTL) | IPC_DIPCCTL_IPCIDIE);
103 
104 		LOG_DBG("Not handled: IPC_DIPCCTL_IPCIDIE");
105 
106 		/* TODO: implement queued message sending if needed */
107 	}
108 }
109 
ipm_adsp_send(const struct device * dev,int wait,uint32_t id,const void * data,int size)110 static int ipm_adsp_send(const struct device *dev, int wait, uint32_t id,
111 			 const void *data, int size)
112 {
113 	LOG_DBG("Send: id %d data %p size %d", id, data, size);
114 	LOG_HEXDUMP_DBG(data, size, "send");
115 
116 	if (id > IPM_INTEL_ADSP_MAX_ID_VAL) {
117 		return -EINVAL;
118 	}
119 
120 	if (size > IPM_INTEL_ADSP_MAX_DATA_SIZE) {
121 		return -EMSGSIZE;
122 	}
123 
124 	if (wait) {
125 		while (ipc_read(IPC_DIPCI) & IPC_DIPCI_BUSY) {
126 		}
127 	} else {
128 		if (ipc_read(IPC_DIPCI) & IPC_DIPCI_BUSY) {
129 			LOG_DBG("Busy: previous message is not handled");
130 			return -EBUSY;
131 		}
132 	}
133 
134 	memcpy((void *)IPM_INTEL_ADSP_MAILBOX_OUT, data, size);
135 	SOC_DCACHE_FLUSH((void *)IPM_INTEL_ADSP_MAILBOX_OUT, size);
136 
137 	ipc_write(IPC_DIPCIE, 0);
138 	ipc_write(IPC_DIPCI, IPC_DIPCI_BUSY | id);
139 
140 	return 0;
141 }
142 
ipm_adsp_register_callback(const struct device * dev,ipm_callback_t cb,void * user_data)143 static void ipm_adsp_register_callback(const struct device *dev,
144 				       ipm_callback_t cb,
145 				       void *user_data)
146 {
147 	struct ipm_adsp_data *data = dev->data;
148 
149 	data->callback = cb;
150 	data->user_data = user_data;
151 }
152 
ipm_adsp_max_data_size_get(const struct device * dev)153 static int ipm_adsp_max_data_size_get(const struct device *dev)
154 {
155 	ARG_UNUSED(dev);
156 
157 	LOG_DBG("dev %p", dev);
158 
159 	return IPM_INTEL_ADSP_MAX_DATA_SIZE;
160 }
161 
ipm_adsp_max_id_val_get(const struct device * dev)162 static uint32_t ipm_adsp_max_id_val_get(const struct device *dev)
163 {
164 	ARG_UNUSED(dev);
165 
166 	LOG_DBG("dev %p", dev);
167 
168 	return IPM_INTEL_ADSP_MAX_ID_VAL;
169 }
170 
ipm_adsp_set_enabled(const struct device * dev,int enable)171 static int ipm_adsp_set_enabled(const struct device *dev, int enable)
172 {
173 	LOG_DBG("dev %p", dev);
174 
175 	/* enable IPC interrupts from host */
176 	ipc_write(IPC_DIPCCTL, IPC_DIPCCTL_IPCIDIE | IPC_DIPCCTL_IPCTBIE);
177 
178 	return 0;
179 }
180 
ipm_adsp_init(const struct device * dev)181 static int ipm_adsp_init(const struct device *dev)
182 {
183 	const struct ipm_adsp_config *config = dev->config;
184 
185 	LOG_DBG("dev %p", dev);
186 
187 	config->irq_config_func(dev);
188 
189 	return 0;
190 }
191 
192 static const struct ipm_driver_api ipm_adsp_driver_api = {
193 	.send = ipm_adsp_send,
194 	.register_callback = ipm_adsp_register_callback,
195 	.max_data_size_get = ipm_adsp_max_data_size_get,
196 	.max_id_val_get = ipm_adsp_max_id_val_get,
197 	.set_enabled = ipm_adsp_set_enabled,
198 };
199 
200 static void ipm_adsp_config_func(const struct device *dev);
201 
202 static const struct ipm_adsp_config ipm_adsp_config = {
203 	.irq_config_func = ipm_adsp_config_func,
204 };
205 
206 static struct ipm_adsp_data ipm_adsp_data;
207 
208 DEVICE_DT_INST_DEFINE(0,
209 		    &ipm_adsp_init,
210 		    NULL,
211 		    &ipm_adsp_data, &ipm_adsp_config,
212 		    PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
213 		    &ipm_adsp_driver_api);
214 
ipm_adsp_config_func(const struct device * dev)215 static void ipm_adsp_config_func(const struct device *dev)
216 {
217 	IRQ_CONNECT(DT_INST_IRQN(0),
218 		    DT_INST_IRQ(0, priority),
219 		    ipm_adsp_isr, DEVICE_DT_INST_GET(0), 0);
220 
221 	irq_enable(DT_INST_IRQN(0));
222 }
223