mtk_scp_ipi.c 5.2 KB

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