1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2019 MediaTek Inc.
5 #include <asm/barrier.h>
9 #include <linux/iopoll.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/time64.h>
14 #include <linux/remoteproc/mtk_scp.h>
16 #include "mtk_common.h"
18 #define SCP_TIMEOUT_US (2000 * USEC_PER_MSEC)
21 * scp_ipi_register() - register an ipi function
23 * @scp: mtk_scp structure
25 * @handler: IPI handler
26 * @priv: private data for IPI handler
28 * Register an ipi function to receive ipi interrupt from SCP.
30 * Return: 0 if ipi registers successfully, -error on error.
32 int scp_ipi_register(struct mtk_scp
*scp
,
34 scp_ipi_handler_t handler
,
40 if (WARN_ON(id
>= SCP_IPI_MAX
) || WARN_ON(handler
== NULL
))
43 scp_ipi_lock(scp
, id
);
44 scp
->ipi_desc
[id
].handler
= handler
;
45 scp
->ipi_desc
[id
].priv
= priv
;
46 scp_ipi_unlock(scp
, id
);
50 EXPORT_SYMBOL_GPL(scp_ipi_register
);
53 * scp_ipi_unregister() - unregister an ipi function
55 * @scp: mtk_scp structure
58 * Unregister an ipi function to receive ipi interrupt from SCP.
60 void scp_ipi_unregister(struct mtk_scp
*scp
, u32 id
)
65 if (WARN_ON(id
>= SCP_IPI_MAX
))
68 scp_ipi_lock(scp
, id
);
69 scp
->ipi_desc
[id
].handler
= NULL
;
70 scp
->ipi_desc
[id
].priv
= NULL
;
71 scp_ipi_unlock(scp
, id
);
73 EXPORT_SYMBOL_GPL(scp_ipi_unregister
);
76 * scp_memcpy_aligned() - Copy src to dst, where dst is in SCP SRAM region.
78 * @dst: Pointer to the destination buffer, should be in SCP SRAM region.
79 * @src: Pointer to the source buffer.
80 * @len: Length of the source buffer to be copied.
82 * Since AP access of SCP SRAM don't support byte write, this always write a
83 * full word at a time, and may cause some extra bytes to be written at the
84 * beginning & ending of dst.
86 void scp_memcpy_aligned(void __iomem
*dst
, const void *src
, unsigned int len
)
90 unsigned int i
= 0, remain
;
92 if (!IS_ALIGNED((unsigned long)dst
, 4)) {
93 ptr
= (void __iomem
*)ALIGN_DOWN((unsigned long)dst
, 4);
95 val
= readl_relaxed(ptr
);
96 memcpy((u8
*)&val
+ (4 - i
), src
, i
);
97 writel_relaxed(val
, ptr
);
100 __iowrite32_copy(dst
+ i
, src
+ i
, (len
- i
) / 4);
101 remain
= (len
- i
) % 4;
104 val
= readl_relaxed(dst
+ len
- remain
);
105 memcpy(&val
, src
+ len
- remain
, remain
);
106 writel_relaxed(val
, dst
+ len
- remain
);
109 EXPORT_SYMBOL_GPL(scp_memcpy_aligned
);
112 * scp_ipi_lock() - Lock before operations of an IPI ID
114 * @scp: mtk_scp structure
117 * Note: This should not be used by drivers other than mtk_scp.
119 void scp_ipi_lock(struct mtk_scp
*scp
, u32 id
)
121 if (WARN_ON(id
>= SCP_IPI_MAX
))
123 mutex_lock(&scp
->ipi_desc
[id
].lock
);
125 EXPORT_SYMBOL_GPL(scp_ipi_lock
);
128 * scp_ipi_unlock() - Unlock after operations of an IPI ID
130 * @scp: mtk_scp structure
133 * Note: This should not be used by drivers other than mtk_scp.
135 void scp_ipi_unlock(struct mtk_scp
*scp
, u32 id
)
137 if (WARN_ON(id
>= SCP_IPI_MAX
))
139 mutex_unlock(&scp
->ipi_desc
[id
].lock
);
141 EXPORT_SYMBOL_GPL(scp_ipi_unlock
);
144 * scp_ipi_send() - send data from AP to scp.
146 * @scp: mtk_scp structure
148 * @buf: the data buffer
149 * @len: the data buffer length
150 * @wait: number of msecs to wait for ack. 0 to skip waiting.
152 * This function is thread-safe. When this function returns,
153 * SCP has received the data and starts the processing.
154 * When the processing completes, IPI handler registered
155 * by scp_ipi_register will be called in interrupt context.
157 * Return: 0 if sending data successfully, -error on error.
159 int scp_ipi_send(struct mtk_scp
*scp
, u32 id
, void *buf
, unsigned int len
,
162 struct mtk_share_obj __iomem
*send_obj
= scp
->send_buf
;
165 const struct mtk_scp_sizes_data
*scp_sizes
;
167 scp_sizes
= scp
->data
->scp_sizes
;
169 if (WARN_ON(id
<= SCP_IPI_INIT
) || WARN_ON(id
>= SCP_IPI_MAX
) ||
170 WARN_ON(id
== SCP_IPI_NS_SERVICE
) ||
171 WARN_ON(len
> scp_sizes
->ipi_share_buffer_size
) || WARN_ON(!buf
))
174 ret
= clk_prepare_enable(scp
->clk
);
176 dev_err(scp
->dev
, "failed to enable clock\n");
180 mutex_lock(&scp
->send_lock
);
182 /* Wait until SCP receives the last command */
183 ret
= readl_poll_timeout_atomic(scp
->cluster
->reg_base
+ scp
->data
->host_to_scp_reg
,
184 val
, !val
, 0, SCP_TIMEOUT_US
);
186 dev_err(scp
->dev
, "%s: IPI timeout!\n", __func__
);
190 scp_memcpy_aligned(&send_obj
->share_buf
, buf
, len
);
192 writel(len
, &send_obj
->len
);
193 writel(id
, &send_obj
->id
);
195 scp
->ipi_id_ack
[id
] = false;
196 /* send the command to SCP */
197 writel(scp
->data
->host_to_scp_int_bit
,
198 scp
->cluster
->reg_base
+ scp
->data
->host_to_scp_reg
);
201 /* wait for SCP's ACK */
202 ret
= wait_event_timeout(scp
->ack_wq
,
204 msecs_to_jiffies(wait
));
205 scp
->ipi_id_ack
[id
] = false;
206 if (WARN(!ret
, "scp ipi %d ack time out !", id
))
213 mutex_unlock(&scp
->send_lock
);
214 clk_disable_unprepare(scp
->clk
);
218 EXPORT_SYMBOL_GPL(scp_ipi_send
);
220 MODULE_LICENSE("GPL v2");
221 MODULE_DESCRIPTION("MediaTek scp IPI interface");