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
,
36 if (WARN_ON(id
>= SCP_IPI_MAX
) || WARN_ON(handler
== NULL
))
39 scp_ipi_lock(scp
, id
);
40 scp
->ipi_desc
[id
].handler
= handler
;
41 scp
->ipi_desc
[id
].priv
= priv
;
42 scp_ipi_unlock(scp
, id
);
46 EXPORT_SYMBOL_GPL(scp_ipi_register
);
49 * scp_ipi_unregister() - unregister an ipi function
51 * @scp: mtk_scp structure
54 * Unregister an ipi function to receive ipi interrupt from SCP.
56 void scp_ipi_unregister(struct mtk_scp
*scp
, u32 id
)
61 if (WARN_ON(id
>= SCP_IPI_MAX
))
64 scp_ipi_lock(scp
, id
);
65 scp
->ipi_desc
[id
].handler
= NULL
;
66 scp
->ipi_desc
[id
].priv
= NULL
;
67 scp_ipi_unlock(scp
, id
);
69 EXPORT_SYMBOL_GPL(scp_ipi_unregister
);
72 * scp_memcpy_aligned() - Copy src to dst, where dst is in SCP SRAM region.
74 * @dst: Pointer to the destination buffer, should be in SCP SRAM region.
75 * @src: Pointer to the source buffer.
76 * @len: Length of the source buffer to be copied.
78 * Since AP access of SCP SRAM don't support byte write, this always write a
79 * full word at a time, and may cause some extra bytes to be written at the
80 * beginning & ending of dst.
82 void scp_memcpy_aligned(void __iomem
*dst
, const void *src
, unsigned int len
)
86 unsigned int i
= 0, remain
;
88 if (!IS_ALIGNED((unsigned long)dst
, 4)) {
89 ptr
= (void __iomem
*)ALIGN_DOWN((unsigned long)dst
, 4);
91 val
= readl_relaxed(ptr
);
92 memcpy((u8
*)&val
+ (4 - i
), src
, i
);
93 writel_relaxed(val
, ptr
);
96 __iowrite32_copy(dst
+ i
, src
+ i
, (len
- i
) / 4);
97 remain
= (len
- i
) % 4;
100 val
= readl_relaxed(dst
+ len
- remain
);
101 memcpy(&val
, src
+ len
- remain
, remain
);
102 writel_relaxed(val
, dst
+ len
- remain
);
105 EXPORT_SYMBOL_GPL(scp_memcpy_aligned
);
108 * scp_ipi_lock() - Lock before operations of an IPI ID
110 * @scp: mtk_scp structure
113 * Note: This should not be used by drivers other than mtk_scp.
115 void scp_ipi_lock(struct mtk_scp
*scp
, u32 id
)
117 if (WARN_ON(id
>= SCP_IPI_MAX
))
119 mutex_lock(&scp
->ipi_desc
[id
].lock
);
121 EXPORT_SYMBOL_GPL(scp_ipi_lock
);
124 * scp_ipi_lock() - Unlock after operations of an IPI ID
126 * @scp: mtk_scp structure
129 * Note: This should not be used by drivers other than mtk_scp.
131 void scp_ipi_unlock(struct mtk_scp
*scp
, u32 id
)
133 if (WARN_ON(id
>= SCP_IPI_MAX
))
135 mutex_unlock(&scp
->ipi_desc
[id
].lock
);
137 EXPORT_SYMBOL_GPL(scp_ipi_unlock
);
140 * scp_ipi_send() - send data from AP to scp.
142 * @scp: mtk_scp structure
144 * @buf: the data buffer
145 * @len: the data buffer length
146 * @wait: number of msecs to wait for ack. 0 to skip waiting.
148 * This function is thread-safe. When this function returns,
149 * SCP has received the data and starts the processing.
150 * When the processing completes, IPI handler registered
151 * by scp_ipi_register will be called in interrupt context.
153 * Returns 0 if sending data successfully, -error on error.
155 int scp_ipi_send(struct mtk_scp
*scp
, u32 id
, void *buf
, unsigned int len
,
158 struct mtk_share_obj __iomem
*send_obj
= scp
->send_buf
;
159 unsigned long timeout
;
162 if (WARN_ON(id
<= SCP_IPI_INIT
) || WARN_ON(id
>= SCP_IPI_MAX
) ||
163 WARN_ON(id
== SCP_IPI_NS_SERVICE
) ||
164 WARN_ON(len
> sizeof(send_obj
->share_buf
)) || WARN_ON(!buf
))
167 mutex_lock(&scp
->send_lock
);
169 ret
= clk_prepare_enable(scp
->clk
);
171 dev_err(scp
->dev
, "failed to enable clock\n");
175 /* Wait until SCP receives the last command */
176 timeout
= jiffies
+ msecs_to_jiffies(2000);
178 if (time_after(jiffies
, timeout
)) {
179 dev_err(scp
->dev
, "%s: IPI timeout!\n", __func__
);
183 } while (readl(scp
->reg_base
+ scp
->data
->host_to_scp_reg
));
185 scp_memcpy_aligned(send_obj
->share_buf
, buf
, len
);
187 writel(len
, &send_obj
->len
);
188 writel(id
, &send_obj
->id
);
190 scp
->ipi_id_ack
[id
] = false;
191 /* send the command to SCP */
192 writel(scp
->data
->host_to_scp_int_bit
,
193 scp
->reg_base
+ scp
->data
->host_to_scp_reg
);
196 /* wait for SCP's ACK */
197 timeout
= msecs_to_jiffies(wait
);
198 ret
= wait_event_timeout(scp
->ack_wq
,
201 scp
->ipi_id_ack
[id
] = false;
202 if (WARN(!ret
, "scp ipi %d ack time out !", id
))
209 clk_disable_unprepare(scp
->clk
);
211 mutex_unlock(&scp
->send_lock
);
215 EXPORT_SYMBOL_GPL(scp_ipi_send
);
217 MODULE_LICENSE("GPL v2");
218 MODULE_DESCRIPTION("MediaTek scp IPI interface");