imx-scu.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 NXP
  4. * Author: Dong Aisheng <aisheng.dong@nxp.com>
  5. *
  6. * Implementation of the SCU IPC functions using MUs (client side).
  7. *
  8. */
  9. #include <linux/err.h>
  10. #include <linux/firmware/imx/types.h>
  11. #include <linux/firmware/imx/ipc.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mailbox_client.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #define SCU_MU_CHAN_NUM 8
  21. #define MAX_RX_TIMEOUT (msecs_to_jiffies(30))
  22. struct imx_sc_chan {
  23. struct imx_sc_ipc *sc_ipc;
  24. struct mbox_client cl;
  25. struct mbox_chan *ch;
  26. int idx;
  27. };
  28. struct imx_sc_ipc {
  29. /* SCU uses 4 Tx and 4 Rx channels */
  30. struct imx_sc_chan chans[SCU_MU_CHAN_NUM];
  31. struct device *dev;
  32. struct mutex lock;
  33. struct completion done;
  34. /* temporarily store the SCU msg */
  35. u32 *msg;
  36. u8 rx_size;
  37. u8 count;
  38. };
  39. /*
  40. * This type is used to indicate error response for most functions.
  41. */
  42. enum imx_sc_error_codes {
  43. IMX_SC_ERR_NONE = 0, /* Success */
  44. IMX_SC_ERR_VERSION = 1, /* Incompatible API version */
  45. IMX_SC_ERR_CONFIG = 2, /* Configuration error */
  46. IMX_SC_ERR_PARM = 3, /* Bad parameter */
  47. IMX_SC_ERR_NOACCESS = 4, /* Permission error (no access) */
  48. IMX_SC_ERR_LOCKED = 5, /* Permission error (locked) */
  49. IMX_SC_ERR_UNAVAILABLE = 6, /* Unavailable (out of resources) */
  50. IMX_SC_ERR_NOTFOUND = 7, /* Not found */
  51. IMX_SC_ERR_NOPOWER = 8, /* No power */
  52. IMX_SC_ERR_IPC = 9, /* Generic IPC error */
  53. IMX_SC_ERR_BUSY = 10, /* Resource is currently busy/active */
  54. IMX_SC_ERR_FAIL = 11, /* General I/O failure */
  55. IMX_SC_ERR_LAST
  56. };
  57. static int imx_sc_linux_errmap[IMX_SC_ERR_LAST] = {
  58. 0, /* IMX_SC_ERR_NONE */
  59. -EINVAL, /* IMX_SC_ERR_VERSION */
  60. -EINVAL, /* IMX_SC_ERR_CONFIG */
  61. -EINVAL, /* IMX_SC_ERR_PARM */
  62. -EACCES, /* IMX_SC_ERR_NOACCESS */
  63. -EACCES, /* IMX_SC_ERR_LOCKED */
  64. -ERANGE, /* IMX_SC_ERR_UNAVAILABLE */
  65. -EEXIST, /* IMX_SC_ERR_NOTFOUND */
  66. -EPERM, /* IMX_SC_ERR_NOPOWER */
  67. -EPIPE, /* IMX_SC_ERR_IPC */
  68. -EBUSY, /* IMX_SC_ERR_BUSY */
  69. -EIO, /* IMX_SC_ERR_FAIL */
  70. };
  71. static struct imx_sc_ipc *imx_sc_ipc_handle;
  72. static inline int imx_sc_to_linux_errno(int errno)
  73. {
  74. if (errno >= IMX_SC_ERR_NONE && errno < IMX_SC_ERR_LAST)
  75. return imx_sc_linux_errmap[errno];
  76. return -EIO;
  77. }
  78. /*
  79. * Get the default handle used by SCU
  80. */
  81. int imx_scu_get_handle(struct imx_sc_ipc **ipc)
  82. {
  83. if (!imx_sc_ipc_handle)
  84. return -EPROBE_DEFER;
  85. *ipc = imx_sc_ipc_handle;
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(imx_scu_get_handle);
  89. static void imx_scu_rx_callback(struct mbox_client *c, void *msg)
  90. {
  91. struct imx_sc_chan *sc_chan = container_of(c, struct imx_sc_chan, cl);
  92. struct imx_sc_ipc *sc_ipc = sc_chan->sc_ipc;
  93. struct imx_sc_rpc_msg *hdr;
  94. u32 *data = msg;
  95. if (sc_chan->idx == 0) {
  96. hdr = msg;
  97. sc_ipc->rx_size = hdr->size;
  98. dev_dbg(sc_ipc->dev, "msg rx size %u\n", sc_ipc->rx_size);
  99. if (sc_ipc->rx_size > 4)
  100. dev_warn(sc_ipc->dev, "RPC does not support receiving over 4 words: %u\n",
  101. sc_ipc->rx_size);
  102. }
  103. sc_ipc->msg[sc_chan->idx] = *data;
  104. sc_ipc->count++;
  105. dev_dbg(sc_ipc->dev, "mu %u msg %u 0x%x\n", sc_chan->idx,
  106. sc_ipc->count, *data);
  107. if ((sc_ipc->rx_size != 0) && (sc_ipc->count == sc_ipc->rx_size))
  108. complete(&sc_ipc->done);
  109. }
  110. static int imx_scu_ipc_write(struct imx_sc_ipc *sc_ipc, void *msg)
  111. {
  112. struct imx_sc_rpc_msg *hdr = msg;
  113. struct imx_sc_chan *sc_chan;
  114. u32 *data = msg;
  115. int ret;
  116. int i;
  117. /* Check size */
  118. if (hdr->size > IMX_SC_RPC_MAX_MSG)
  119. return -EINVAL;
  120. dev_dbg(sc_ipc->dev, "RPC SVC %u FUNC %u SIZE %u\n", hdr->svc,
  121. hdr->func, hdr->size);
  122. for (i = 0; i < hdr->size; i++) {
  123. sc_chan = &sc_ipc->chans[i % 4];
  124. ret = mbox_send_message(sc_chan->ch, &data[i]);
  125. if (ret < 0)
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. /*
  131. * RPC command/response
  132. */
  133. int imx_scu_call_rpc(struct imx_sc_ipc *sc_ipc, void *msg, bool have_resp)
  134. {
  135. struct imx_sc_rpc_msg *hdr;
  136. int ret;
  137. if (WARN_ON(!sc_ipc || !msg))
  138. return -EINVAL;
  139. mutex_lock(&sc_ipc->lock);
  140. reinit_completion(&sc_ipc->done);
  141. sc_ipc->msg = msg;
  142. sc_ipc->count = 0;
  143. ret = imx_scu_ipc_write(sc_ipc, msg);
  144. if (ret < 0) {
  145. dev_err(sc_ipc->dev, "RPC send msg failed: %d\n", ret);
  146. goto out;
  147. }
  148. if (have_resp) {
  149. if (!wait_for_completion_timeout(&sc_ipc->done,
  150. MAX_RX_TIMEOUT)) {
  151. dev_err(sc_ipc->dev, "RPC send msg timeout\n");
  152. mutex_unlock(&sc_ipc->lock);
  153. return -ETIMEDOUT;
  154. }
  155. /* response status is stored in hdr->func field */
  156. hdr = msg;
  157. ret = hdr->func;
  158. }
  159. out:
  160. mutex_unlock(&sc_ipc->lock);
  161. dev_dbg(sc_ipc->dev, "RPC SVC done\n");
  162. return imx_sc_to_linux_errno(ret);
  163. }
  164. EXPORT_SYMBOL(imx_scu_call_rpc);
  165. static int imx_scu_probe(struct platform_device *pdev)
  166. {
  167. struct device *dev = &pdev->dev;
  168. struct imx_sc_ipc *sc_ipc;
  169. struct imx_sc_chan *sc_chan;
  170. struct mbox_client *cl;
  171. char *chan_name;
  172. int ret;
  173. int i;
  174. sc_ipc = devm_kzalloc(dev, sizeof(*sc_ipc), GFP_KERNEL);
  175. if (!sc_ipc)
  176. return -ENOMEM;
  177. for (i = 0; i < SCU_MU_CHAN_NUM; i++) {
  178. if (i < 4)
  179. chan_name = kasprintf(GFP_KERNEL, "tx%d", i);
  180. else
  181. chan_name = kasprintf(GFP_KERNEL, "rx%d", i - 4);
  182. if (!chan_name)
  183. return -ENOMEM;
  184. sc_chan = &sc_ipc->chans[i];
  185. cl = &sc_chan->cl;
  186. cl->dev = dev;
  187. cl->tx_block = false;
  188. cl->knows_txdone = true;
  189. cl->rx_callback = imx_scu_rx_callback;
  190. sc_chan->sc_ipc = sc_ipc;
  191. sc_chan->idx = i % 4;
  192. sc_chan->ch = mbox_request_channel_byname(cl, chan_name);
  193. if (IS_ERR(sc_chan->ch)) {
  194. ret = PTR_ERR(sc_chan->ch);
  195. if (ret != -EPROBE_DEFER)
  196. dev_err(dev, "Failed to request mbox chan %s ret %d\n",
  197. chan_name, ret);
  198. return ret;
  199. }
  200. dev_dbg(dev, "request mbox chan %s\n", chan_name);
  201. /* chan_name is not used anymore by framework */
  202. kfree(chan_name);
  203. }
  204. sc_ipc->dev = dev;
  205. mutex_init(&sc_ipc->lock);
  206. init_completion(&sc_ipc->done);
  207. imx_sc_ipc_handle = sc_ipc;
  208. dev_info(dev, "NXP i.MX SCU Initialized\n");
  209. return devm_of_platform_populate(dev);
  210. }
  211. static const struct of_device_id imx_scu_match[] = {
  212. { .compatible = "fsl,imx-scu", },
  213. { /* Sentinel */ }
  214. };
  215. static struct platform_driver imx_scu_driver = {
  216. .driver = {
  217. .name = "imx-scu",
  218. .of_match_table = imx_scu_match,
  219. },
  220. .probe = imx_scu_probe,
  221. };
  222. builtin_platform_driver(imx_scu_driver);
  223. MODULE_AUTHOR("Dong Aisheng <aisheng.dong@nxp.com>");
  224. MODULE_DESCRIPTION("IMX SCU firmware protocol driver");
  225. MODULE_LICENSE("GPL v2");