linux/drivers/remoteproc/mtk_scp_ipi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// Copyright (c) 2019 MediaTek Inc.
   4
   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>
  13
  14#include "mtk_common.h"
  15
  16/**
  17 * scp_ipi_register() - register an ipi function
  18 *
  19 * @scp:        mtk_scp structure
  20 * @id:         IPI ID
  21 * @handler:    IPI handler
  22 * @priv:       private data for IPI handler
  23 *
  24 * Register an ipi function to receive ipi interrupt from SCP.
  25 *
  26 * Returns 0 if ipi registers successfully, -error on error.
  27 */
  28int scp_ipi_register(struct mtk_scp *scp,
  29                     u32 id,
  30                     scp_ipi_handler_t handler,
  31                     void *priv)
  32{
  33        if (!scp) {
  34                dev_err(scp->dev, "scp device is not ready\n");
  35                return -EPROBE_DEFER;
  36        }
  37
  38        if (WARN_ON(id >= SCP_IPI_MAX) || WARN_ON(handler == NULL))
  39                return -EINVAL;
  40
  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);
  45
  46        return 0;
  47}
  48EXPORT_SYMBOL_GPL(scp_ipi_register);
  49
  50/**
  51 * scp_ipi_unregister() - unregister an ipi function
  52 *
  53 * @scp:        mtk_scp structure
  54 * @id:         IPI ID
  55 *
  56 * Unregister an ipi function to receive ipi interrupt from SCP.
  57 */
  58void scp_ipi_unregister(struct mtk_scp *scp, u32 id)
  59{
  60        if (!scp)
  61                return;
  62
  63        if (WARN_ON(id >= SCP_IPI_MAX))
  64                return;
  65
  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);
  70}
  71EXPORT_SYMBOL_GPL(scp_ipi_unregister);
  72
  73/*
  74 * scp_memcpy_aligned() - Copy src to dst, where dst is in SCP SRAM region.
  75 *
  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.
  79 *
  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.
  83 */
  84void scp_memcpy_aligned(void __iomem *dst, const void *src, unsigned int len)
  85{
  86        void __iomem *ptr;
  87        u32 val;
  88        unsigned int i = 0, remain;
  89
  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);
  96        }
  97
  98        __iowrite32_copy(dst + i, src + i, (len - i) / 4);
  99        remain = (len - i) % 4;
 100
 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);
 105        }
 106}
 107EXPORT_SYMBOL_GPL(scp_memcpy_aligned);
 108
 109/**
 110 * scp_ipi_lock() - Lock before operations of an IPI ID
 111 *
 112 * @scp:        mtk_scp structure
 113 * @id:         IPI ID
 114 *
 115 * Note: This should not be used by drivers other than mtk_scp.
 116 */
 117void scp_ipi_lock(struct mtk_scp *scp, u32 id)
 118{
 119        if (WARN_ON(id >= SCP_IPI_MAX))
 120                return;
 121        mutex_lock(&scp->ipi_desc[id].lock);
 122}
 123EXPORT_SYMBOL_GPL(scp_ipi_lock);
 124
 125/**
 126 * scp_ipi_lock() - Unlock after operations of an IPI ID
 127 *
 128 * @scp:        mtk_scp structure
 129 * @id:         IPI ID
 130 *
 131 * Note: This should not be used by drivers other than mtk_scp.
 132 */
 133void scp_ipi_unlock(struct mtk_scp *scp, u32 id)
 134{
 135        if (WARN_ON(id >= SCP_IPI_MAX))
 136                return;
 137        mutex_unlock(&scp->ipi_desc[id].lock);
 138}
 139EXPORT_SYMBOL_GPL(scp_ipi_unlock);
 140
 141/**
 142 * scp_ipi_send() - send data from AP to scp.
 143 *
 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.
 149 *
 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.
 154 *
 155 * Returns 0 if sending data successfully, -error on error.
 156 **/
 157int scp_ipi_send(struct mtk_scp *scp, u32 id, void *buf, unsigned int len,
 158                 unsigned int wait)
 159{
 160        struct mtk_share_obj __iomem *send_obj = scp->send_buf;
 161        unsigned long timeout;
 162        int ret;
 163
 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;
 168
 169        mutex_lock(&scp->send_lock);
 170
 171        ret = clk_prepare_enable(scp->clk);
 172        if (ret) {
 173                dev_err(scp->dev, "failed to enable clock\n");
 174                goto unlock_mutex;
 175        }
 176
 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;
 184                }
 185        } while (readl(scp->reg_base + MT8183_HOST_TO_SCP));
 186
 187        scp_memcpy_aligned(send_obj->share_buf, buf, len);
 188
 189        writel(len, &send_obj->len);
 190        writel(id, &send_obj->id);
 191
 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);
 195
 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;
 207        }
 208
 209clock_disable:
 210        clk_disable_unprepare(scp->clk);
 211unlock_mutex:
 212        mutex_unlock(&scp->send_lock);
 213
 214        return ret;
 215}
 216EXPORT_SYMBOL_GPL(scp_ipi_send);
 217
 218MODULE_LICENSE("GPL v2");
 219MODULE_DESCRIPTION("MediaTek scp IPI interface");
 220