gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / remoteproc / mtk_scp_ipi.c
blob3d3d87210ef2c4773db33dd67c96b5bd002a4888
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2019 MediaTek Inc.
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>
14 #include "mtk_common.h"
16 /**
17 * scp_ipi_register() - register an ipi function
19 * @scp: mtk_scp structure
20 * @id: IPI ID
21 * @handler: IPI handler
22 * @priv: private data for IPI handler
24 * Register an ipi function to receive ipi interrupt from SCP.
26 * Returns 0 if ipi registers successfully, -error on error.
28 int scp_ipi_register(struct mtk_scp *scp,
29 u32 id,
30 scp_ipi_handler_t handler,
31 void *priv)
33 if (!scp) {
34 dev_err(scp->dev, "scp device is not ready\n");
35 return -EPROBE_DEFER;
38 if (WARN_ON(id >= SCP_IPI_MAX) || WARN_ON(handler == NULL))
39 return -EINVAL;
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);
46 return 0;
48 EXPORT_SYMBOL_GPL(scp_ipi_register);
50 /**
51 * scp_ipi_unregister() - unregister an ipi function
53 * @scp: mtk_scp structure
54 * @id: IPI ID
56 * Unregister an ipi function to receive ipi interrupt from SCP.
58 void scp_ipi_unregister(struct mtk_scp *scp, u32 id)
60 if (!scp)
61 return;
63 if (WARN_ON(id >= SCP_IPI_MAX))
64 return;
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);
71 EXPORT_SYMBOL_GPL(scp_ipi_unregister);
74 * scp_memcpy_aligned() - Copy src to dst, where dst is in SCP SRAM region.
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.
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.
84 void scp_memcpy_aligned(void __iomem *dst, const void *src, unsigned int len)
86 void __iomem *ptr;
87 u32 val;
88 unsigned int i = 0, remain;
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);
98 __iowrite32_copy(dst + i, src + i, (len - i) / 4);
99 remain = (len - i) % 4;
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);
107 EXPORT_SYMBOL_GPL(scp_memcpy_aligned);
110 * scp_ipi_lock() - Lock before operations of an IPI ID
112 * @scp: mtk_scp structure
113 * @id: IPI ID
115 * Note: This should not be used by drivers other than mtk_scp.
117 void scp_ipi_lock(struct mtk_scp *scp, u32 id)
119 if (WARN_ON(id >= SCP_IPI_MAX))
120 return;
121 mutex_lock(&scp->ipi_desc[id].lock);
123 EXPORT_SYMBOL_GPL(scp_ipi_lock);
126 * scp_ipi_lock() - Unlock after operations of an IPI ID
128 * @scp: mtk_scp structure
129 * @id: IPI ID
131 * Note: This should not be used by drivers other than mtk_scp.
133 void scp_ipi_unlock(struct mtk_scp *scp, u32 id)
135 if (WARN_ON(id >= SCP_IPI_MAX))
136 return;
137 mutex_unlock(&scp->ipi_desc[id].lock);
139 EXPORT_SYMBOL_GPL(scp_ipi_unlock);
142 * scp_ipi_send() - send data from AP to scp.
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.
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.
155 * Returns 0 if sending data successfully, -error on error.
157 int scp_ipi_send(struct mtk_scp *scp, u32 id, void *buf, unsigned int len,
158 unsigned int wait)
160 struct mtk_share_obj __iomem *send_obj = scp->send_buf;
161 unsigned long timeout;
162 int ret;
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;
169 mutex_lock(&scp->send_lock);
171 ret = clk_prepare_enable(scp->clk);
172 if (ret) {
173 dev_err(scp->dev, "failed to enable clock\n");
174 goto unlock_mutex;
177 /* Wait until SCP receives the last command */
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;
185 } while (readl(scp->reg_base + MT8183_HOST_TO_SCP));
187 scp_memcpy_aligned(send_obj->share_buf, buf, len);
189 writel(len, &send_obj->len);
190 writel(id, &send_obj->id);
192 scp->ipi_id_ack[id] = false;
193 /* send the command to SCP */
194 writel(MT8183_HOST_IPC_INT_BIT, scp->reg_base + MT8183_HOST_TO_SCP);
196 if (wait) {
197 /* wait for SCP's ACK */
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;
209 clock_disable:
210 clk_disable_unprepare(scp->clk);
211 unlock_mutex:
212 mutex_unlock(&scp->send_lock);
214 return ret;
216 EXPORT_SYMBOL_GPL(scp_ipi_send);
218 MODULE_LICENSE("GPL v2");
219 MODULE_DESCRIPTION("MediaTek scp IPI interface");