blob: abdb7944368aa55b55b768d4c1fb44da438fdd47 [file] [log] [blame]
Erin Lo63c13d62019-11-12 19:03:25 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (c) 2019 MediaTek Inc.
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 * scp_ipi_register() - register an ipi function
18 *
19 * @scp: mtk_scp structure
20 * @id: IPI ID
21 * @handler: IPI handler
22 * @priv: private data for IPI handler
23 *
24 * Register an ipi function to receive ipi interrupt from SCP.
25 *
26 * Returns 0 if ipi registers successfully, -error on error.
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 * scp_ipi_unregister() - unregister an ipi function
52 *
53 * @scp: mtk_scp structure
54 * @id: IPI ID
55 *
56 * Unregister an ipi function to receive ipi interrupt from SCP.
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 * scp_memcpy_aligned() - Copy src to dst, where dst is in SCP SRAM region.
75 *
76 * @dst: Pointer to the destination buffer, should be in SCP SRAM region.
77 * @src: Pointer to the source buffer.
78 * @len: Length of the source buffer to be copied.
79 *
80 * Since AP access of SCP SRAM don't support byte write, this always write a
81 * full word at a time, and may cause some extra bytes to be written at the
82 * beginning & ending of dst.
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 * scp_ipi_lock() - Lock before operations of an IPI ID
111 *
112 * @scp: mtk_scp structure
113 * @id: IPI ID
114 *
115 * Note: This should not be used by drivers other than mtk_scp.
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 * scp_ipi_lock() - Unlock after operations of an IPI ID
127 *
128 * @scp: mtk_scp structure
129 * @id: IPI ID
130 *
131 * Note: This should not be used by drivers other than mtk_scp.
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 * scp_ipi_send() - send data from AP to scp.
143 *
144 * @scp: mtk_scp structure
145 * @id: IPI ID
146 * @buf: the data buffer
147 * @len: the data buffer length
148 * @wait: number of msecs to wait for ack. 0 to skip waiting.
149 *
150 * This function is thread-safe. When this function returns,
151 * SCP has received the data and starts the processing.
152 * When the processing completes, IPI handler registered
153 * by scp_ipi_register will be called in interrupt context.
154 *
155 * Returns 0 if sending data successfully, -error on error.
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(len > sizeof(send_obj->share_buf)) || WARN_ON(!buf))
166 return -EINVAL;
167
168 mutex_lock(&scp->send_lock);
169
170 ret = clk_prepare_enable(scp->clk);
171 if (ret) {
172 dev_err(scp->dev, "failed to enable clock\n");
173 goto unlock_mutex;
174 }
175
176 /* Wait until SCP receives the last command */
177 timeout = jiffies + msecs_to_jiffies(2000);
178 do {
179 if (time_after(jiffies, timeout)) {
180 dev_err(scp->dev, "%s: IPI timeout!\n", __func__);
181 ret = -ETIMEDOUT;
182 goto clock_disable;
183 }
184 } while (readl(scp->reg_base + MT8183_HOST_TO_SCP));
185
186 scp_memcpy_aligned(send_obj->share_buf, buf, len);
187
188 writel(len, &send_obj->len);
189 writel(id, &send_obj->id);
190
191 scp->ipi_id_ack[id] = false;
192 /* send the command to SCP */
193 writel(MT8183_HOST_IPC_INT_BIT, scp->reg_base + MT8183_HOST_TO_SCP);
194
195 if (wait) {
196 /* wait for SCP's ACK */
197 timeout = msecs_to_jiffies(wait);
198 ret = wait_event_timeout(scp->ack_wq,
199 scp->ipi_id_ack[id],
200 timeout);
201 scp->ipi_id_ack[id] = false;
202 if (WARN(!ret, "scp ipi %d ack time out !", id))
203 ret = -EIO;
204 else
205 ret = 0;
206 }
207
208clock_disable:
209 clk_disable_unprepare(scp->clk);
210unlock_mutex:
211 mutex_unlock(&scp->send_lock);
212
213 return ret;
214}
215EXPORT_SYMBOL_GPL(scp_ipi_send);
216
217MODULE_LICENSE("GPL v2");
218MODULE_DESCRIPTION("MediaTek scp IPI interface");