^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // Copyright (c) 2019 MediaTek Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <asm/barrier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/remoteproc/mtk_scp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "mtk_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * scp_ipi_register() - register an ipi function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @scp: mtk_scp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @id: IPI ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @handler: IPI handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @priv: private data for IPI handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Register an ipi function to receive ipi interrupt from SCP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Returns 0 if ipi registers successfully, -error on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int scp_ipi_register(struct mtk_scp *scp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) scp_ipi_handler_t handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (!scp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (WARN_ON(id >= SCP_IPI_MAX) || WARN_ON(handler == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) scp_ipi_lock(scp, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) scp->ipi_desc[id].handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) scp->ipi_desc[id].priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) scp_ipi_unlock(scp, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) EXPORT_SYMBOL_GPL(scp_ipi_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * scp_ipi_unregister() - unregister an ipi function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @scp: mtk_scp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @id: IPI ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Unregister an ipi function to receive ipi interrupt from SCP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void scp_ipi_unregister(struct mtk_scp *scp, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!scp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (WARN_ON(id >= SCP_IPI_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) scp_ipi_lock(scp, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) scp->ipi_desc[id].handler = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) scp->ipi_desc[id].priv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) scp_ipi_unlock(scp, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) EXPORT_SYMBOL_GPL(scp_ipi_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * scp_memcpy_aligned() - Copy src to dst, where dst is in SCP SRAM region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * @dst: Pointer to the destination buffer, should be in SCP SRAM region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * @src: Pointer to the source buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * @len: Length of the source buffer to be copied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * Since AP access of SCP SRAM don't support byte write, this always write a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * full word at a time, and may cause some extra bytes to be written at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * beginning & ending of dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) void scp_memcpy_aligned(void __iomem *dst, const void *src, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) void __iomem *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned int i = 0, remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!IS_ALIGNED((unsigned long)dst, 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ptr = (void __iomem *)ALIGN_DOWN((unsigned long)dst, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) i = 4 - (dst - ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) val = readl_relaxed(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) memcpy((u8 *)&val + (4 - i), src, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) writel_relaxed(val, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) __iowrite32_copy(dst + i, src + i, (len - i) / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) remain = (len - i) % 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (remain > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) val = readl_relaxed(dst + len - remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) memcpy(&val, src + len - remain, remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) writel_relaxed(val, dst + len - remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) EXPORT_SYMBOL_GPL(scp_memcpy_aligned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * scp_ipi_lock() - Lock before operations of an IPI ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * @scp: mtk_scp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * @id: IPI ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Note: This should not be used by drivers other than mtk_scp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) void scp_ipi_lock(struct mtk_scp *scp, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (WARN_ON(id >= SCP_IPI_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) mutex_lock(&scp->ipi_desc[id].lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) EXPORT_SYMBOL_GPL(scp_ipi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * scp_ipi_lock() - Unlock after operations of an IPI ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * @scp: mtk_scp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * @id: IPI ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * Note: This should not be used by drivers other than mtk_scp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void scp_ipi_unlock(struct mtk_scp *scp, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (WARN_ON(id >= SCP_IPI_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) mutex_unlock(&scp->ipi_desc[id].lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) EXPORT_SYMBOL_GPL(scp_ipi_unlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * scp_ipi_send() - send data from AP to scp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @scp: mtk_scp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * @id: IPI ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * @buf: the data buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @len: the data buffer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * @wait: number of msecs to wait for ack. 0 to skip waiting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * This function is thread-safe. When this function returns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * SCP has received the data and starts the processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * When the processing completes, IPI handler registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * by scp_ipi_register will be called in interrupt context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * Returns 0 if sending data successfully, -error on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int scp_ipi_send(struct mtk_scp *scp, u32 id, void *buf, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) unsigned int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct mtk_share_obj __iomem *send_obj = scp->send_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (WARN_ON(id <= SCP_IPI_INIT) || WARN_ON(id >= SCP_IPI_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) WARN_ON(id == SCP_IPI_NS_SERVICE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) WARN_ON(len > sizeof(send_obj->share_buf)) || WARN_ON(!buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) mutex_lock(&scp->send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ret = clk_prepare_enable(scp->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) dev_err(scp->dev, "failed to enable clock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /* Wait until SCP receives the last command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) timeout = jiffies + msecs_to_jiffies(2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (time_after(jiffies, timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) dev_err(scp->dev, "%s: IPI timeout!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto clock_disable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) } while (readl(scp->reg_base + scp->data->host_to_scp_reg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) scp_memcpy_aligned(send_obj->share_buf, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) writel(len, &send_obj->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) writel(id, &send_obj->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) scp->ipi_id_ack[id] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* send the command to SCP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) writel(scp->data->host_to_scp_int_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) scp->reg_base + scp->data->host_to_scp_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (wait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* wait for SCP's ACK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) timeout = msecs_to_jiffies(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ret = wait_event_timeout(scp->ack_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) scp->ipi_id_ack[id],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) scp->ipi_id_ack[id] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (WARN(!ret, "scp ipi %d ack time out !", id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) clock_disable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) clk_disable_unprepare(scp->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) unlock_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) mutex_unlock(&scp->send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) EXPORT_SYMBOL_GPL(scp_ipi_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) MODULE_DESCRIPTION("MediaTek scp IPI interface");