mtk_scp_ipi.c 5.3 KB

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