1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2019 MediaTek Inc.
5 #include <asm/barrier.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"
17 * scp_ipi_register() - register an ipi function
19 * @scp: mtk_scp structure
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
,
30 scp_ipi_handler_t handler
,
34 dev_err(scp
->dev
, "scp device is not ready\n");
38 if (WARN_ON(id
>= SCP_IPI_MAX
) || WARN_ON(handler
== NULL
))
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
);
48 EXPORT_SYMBOL_GPL(scp_ipi_register
);
51 * scp_ipi_unregister() - unregister an ipi function
53 * @scp: mtk_scp structure
56 * Unregister an ipi function to receive ipi interrupt from SCP.
58 void scp_ipi_unregister(struct mtk_scp
*scp
, u32 id
)
63 if (WARN_ON(id
>= SCP_IPI_MAX
))
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
)
88 unsigned int i
= 0, remain
;
90 if (!IS_ALIGNED((unsigned long)dst
, 4)) {
91 ptr
= (void __iomem
*)ALIGN_DOWN((unsigned long)dst
, 4);
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;
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
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
))
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
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
))
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
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
,
160 struct mtk_share_obj __iomem
*send_obj
= scp
->send_buf
;
161 unsigned long timeout
;
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
))
169 mutex_lock(&scp
->send_lock
);
171 ret
= clk_prepare_enable(scp
->clk
);
173 dev_err(scp
->dev
, "failed to enable clock\n");
177 /* Wait until SCP receives the last command */
178 timeout
= jiffies
+ msecs_to_jiffies(2000);
180 if (time_after(jiffies
, timeout
)) {
181 dev_err(scp
->dev
, "%s: IPI timeout!\n", __func__
);
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
);
197 /* wait for SCP's ACK */
198 timeout
= msecs_to_jiffies(wait
);
199 ret
= wait_event_timeout(scp
->ack_wq
,
202 scp
->ipi_id_ack
[id
] = false;
203 if (WARN(!ret
, "scp ipi %d ack time out !", id
))
210 clk_disable_unprepare(scp
->clk
);
212 mutex_unlock(&scp
->send_lock
);
216 EXPORT_SYMBOL_GPL(scp_ipi_send
);
218 MODULE_LICENSE("GPL v2");
219 MODULE_DESCRIPTION("MediaTek scp IPI interface");