1
2
3
4
5#include <asm/barrier.h>
6#include <linux/clk.h>
7#include <linux/err.h>
8#include <linux/io.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/remoteproc/mtk_scp.h>
13
14#include "mtk_common.h"
15
16
17
18
19
20
21
22
23
24
25
26
27
28int scp_ipi_register(struct mtk_scp *scp,
29 u32 id,
30 scp_ipi_handler_t handler,
31 void *priv)
32{
33 if (!scp) {
34 dev_err(scp->dev, "scp device is not ready\n");
35 return -EPROBE_DEFER;
36 }
37
38 if (WARN_ON(id >= SCP_IPI_MAX) || WARN_ON(handler == NULL))
39 return -EINVAL;
40
41 scp_ipi_lock(scp, id);
42 scp->ipi_desc[id].handler = handler;
43 scp->ipi_desc[id].priv = priv;
44 scp_ipi_unlock(scp, id);
45
46 return 0;
47}
48EXPORT_SYMBOL_GPL(scp_ipi_register);
49
50
51
52
53
54
55
56
57
58void scp_ipi_unregister(struct mtk_scp *scp, u32 id)
59{
60 if (!scp)
61 return;
62
63 if (WARN_ON(id >= SCP_IPI_MAX))
64 return;
65
66 scp_ipi_lock(scp, id);
67 scp->ipi_desc[id].handler = NULL;
68 scp->ipi_desc[id].priv = NULL;
69 scp_ipi_unlock(scp, id);
70}
71EXPORT_SYMBOL_GPL(scp_ipi_unregister);
72
73
74
75
76
77
78
79
80
81
82
83
84void scp_memcpy_aligned(void __iomem *dst, const void *src, unsigned int len)
85{
86 void __iomem *ptr;
87 u32 val;
88 unsigned int i = 0, remain;
89
90 if (!IS_ALIGNED((unsigned long)dst, 4)) {
91 ptr = (void __iomem *)ALIGN_DOWN((unsigned long)dst, 4);
92 i = 4 - (dst - ptr);
93 val = readl_relaxed(ptr);
94 memcpy((u8 *)&val + (4 - i), src, i);
95 writel_relaxed(val, ptr);
96 }
97
98 __iowrite32_copy(dst + i, src + i, (len - i) / 4);
99 remain = (len - i) % 4;
100
101 if (remain > 0) {
102 val = readl_relaxed(dst + len - remain);
103 memcpy(&val, src + len - remain, remain);
104 writel_relaxed(val, dst + len - remain);
105 }
106}
107EXPORT_SYMBOL_GPL(scp_memcpy_aligned);
108
109
110
111
112
113
114
115
116
117void scp_ipi_lock(struct mtk_scp *scp, u32 id)
118{
119 if (WARN_ON(id >= SCP_IPI_MAX))
120 return;
121 mutex_lock(&scp->ipi_desc[id].lock);
122}
123EXPORT_SYMBOL_GPL(scp_ipi_lock);
124
125
126
127
128
129
130
131
132
133void scp_ipi_unlock(struct mtk_scp *scp, u32 id)
134{
135 if (WARN_ON(id >= SCP_IPI_MAX))
136 return;
137 mutex_unlock(&scp->ipi_desc[id].lock);
138}
139EXPORT_SYMBOL_GPL(scp_ipi_unlock);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157int scp_ipi_send(struct mtk_scp *scp, u32 id, void *buf, unsigned int len,
158 unsigned int wait)
159{
160 struct mtk_share_obj __iomem *send_obj = scp->send_buf;
161 unsigned long timeout;
162 int ret;
163
164 if (WARN_ON(id <= SCP_IPI_INIT) || WARN_ON(id >= SCP_IPI_MAX) ||
165 WARN_ON(id == SCP_IPI_NS_SERVICE) ||
166 WARN_ON(len > sizeof(send_obj->share_buf)) || WARN_ON(!buf))
167 return -EINVAL;
168
169 mutex_lock(&scp->send_lock);
170
171 ret = clk_prepare_enable(scp->clk);
172 if (ret) {
173 dev_err(scp->dev, "failed to enable clock\n");
174 goto unlock_mutex;
175 }
176
177
178 timeout = jiffies + msecs_to_jiffies(2000);
179 do {
180 if (time_after(jiffies, timeout)) {
181 dev_err(scp->dev, "%s: IPI timeout!\n", __func__);
182 ret = -ETIMEDOUT;
183 goto clock_disable;
184 }
185 } while (readl(scp->reg_base + MT8183_HOST_TO_SCP));
186
187 scp_memcpy_aligned(send_obj->share_buf, buf, len);
188
189 writel(len, &send_obj->len);
190 writel(id, &send_obj->id);
191
192 scp->ipi_id_ack[id] = false;
193
194 writel(MT8183_HOST_IPC_INT_BIT, scp->reg_base + MT8183_HOST_TO_SCP);
195
196 if (wait) {
197
198 timeout = msecs_to_jiffies(wait);
199 ret = wait_event_timeout(scp->ack_wq,
200 scp->ipi_id_ack[id],
201 timeout);
202 scp->ipi_id_ack[id] = false;
203 if (WARN(!ret, "scp ipi %d ack time out !", id))
204 ret = -EIO;
205 else
206 ret = 0;
207 }
208
209clock_disable:
210 clk_disable_unprepare(scp->clk);
211unlock_mutex:
212 mutex_unlock(&scp->send_lock);
213
214 return ret;
215}
216EXPORT_SYMBOL_GPL(scp_ipi_send);
217
218MODULE_LICENSE("GPL v2");
219MODULE_DESCRIPTION("MediaTek scp IPI interface");
220