imx-scu.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 NXP
  4. * Author: Dong Aisheng <[email protected]>
  5. *
  6. * Implementation of the SCU IPC functions using MUs (client side).
  7. *
  8. */
  9. #include <linux/err.h>
  10. #include <linux/firmware/imx/ipc.h>
  11. #include <linux/firmware/imx/sci.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. struct completion tx_done;
  28. };
  29. struct imx_sc_ipc {
  30. /* SCU uses 4 Tx and 4 Rx channels */
  31. struct imx_sc_chan chans[SCU_MU_CHAN_NUM];
  32. struct device *dev;
  33. struct mutex lock;
  34. struct completion done;
  35. bool fast_ipc;
  36. /* temporarily store the SCU msg */
  37. u32 *msg;
  38. u8 rx_size;
  39. u8 count;
  40. };
  41. /*
  42. * This type is used to indicate error response for most functions.
  43. */
  44. enum imx_sc_error_codes {
  45. IMX_SC_ERR_NONE = 0, /* Success */
  46. IMX_SC_ERR_VERSION = 1, /* Incompatible API version */
  47. IMX_SC_ERR_CONFIG = 2, /* Configuration error */
  48. IMX_SC_ERR_PARM = 3, /* Bad parameter */
  49. IMX_SC_ERR_NOACCESS = 4, /* Permission error (no access) */
  50. IMX_SC_ERR_LOCKED = 5, /* Permission error (locked) */
  51. IMX_SC_ERR_UNAVAILABLE = 6, /* Unavailable (out of resources) */
  52. IMX_SC_ERR_NOTFOUND = 7, /* Not found */
  53. IMX_SC_ERR_NOPOWER = 8, /* No power */
  54. IMX_SC_ERR_IPC = 9, /* Generic IPC error */
  55. IMX_SC_ERR_BUSY = 10, /* Resource is currently busy/active */
  56. IMX_SC_ERR_FAIL = 11, /* General I/O failure */
  57. IMX_SC_ERR_LAST
  58. };
  59. static int imx_sc_linux_errmap[IMX_SC_ERR_LAST] = {
  60. 0, /* IMX_SC_ERR_NONE */
  61. -EINVAL, /* IMX_SC_ERR_VERSION */
  62. -EINVAL, /* IMX_SC_ERR_CONFIG */
  63. -EINVAL, /* IMX_SC_ERR_PARM */
  64. -EACCES, /* IMX_SC_ERR_NOACCESS */
  65. -EACCES, /* IMX_SC_ERR_LOCKED */
  66. -ERANGE, /* IMX_SC_ERR_UNAVAILABLE */
  67. -EEXIST, /* IMX_SC_ERR_NOTFOUND */
  68. -EPERM, /* IMX_SC_ERR_NOPOWER */
  69. -EPIPE, /* IMX_SC_ERR_IPC */
  70. -EBUSY, /* IMX_SC_ERR_BUSY */
  71. -EIO, /* IMX_SC_ERR_FAIL */
  72. };
  73. static struct imx_sc_ipc *imx_sc_ipc_handle;
  74. static inline int imx_sc_to_linux_errno(int errno)
  75. {
  76. if (errno >= IMX_SC_ERR_NONE && errno < IMX_SC_ERR_LAST)
  77. return imx_sc_linux_errmap[errno];
  78. return -EIO;
  79. }
  80. /*
  81. * Get the default handle used by SCU
  82. */
  83. int imx_scu_get_handle(struct imx_sc_ipc **ipc)
  84. {
  85. if (!imx_sc_ipc_handle)
  86. return -EPROBE_DEFER;
  87. *ipc = imx_sc_ipc_handle;
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(imx_scu_get_handle);
  91. /* Callback called when the word of a message is ack-ed, eg read by SCU */
  92. static void imx_scu_tx_done(struct mbox_client *cl, void *mssg, int r)
  93. {
  94. struct imx_sc_chan *sc_chan = container_of(cl, struct imx_sc_chan, cl);
  95. complete(&sc_chan->tx_done);
  96. }
  97. static void imx_scu_rx_callback(struct mbox_client *c, void *msg)
  98. {
  99. struct imx_sc_chan *sc_chan = container_of(c, struct imx_sc_chan, cl);
  100. struct imx_sc_ipc *sc_ipc = sc_chan->sc_ipc;
  101. struct imx_sc_rpc_msg *hdr;
  102. u32 *data = msg;
  103. int i;
  104. if (!sc_ipc->msg) {
  105. dev_warn(sc_ipc->dev, "unexpected rx idx %d 0x%08x, ignore!\n",
  106. sc_chan->idx, *data);
  107. return;
  108. }
  109. if (sc_ipc->fast_ipc) {
  110. hdr = msg;
  111. sc_ipc->rx_size = hdr->size;
  112. sc_ipc->msg[0] = *data++;
  113. for (i = 1; i < sc_ipc->rx_size; i++)
  114. sc_ipc->msg[i] = *data++;
  115. complete(&sc_ipc->done);
  116. return;
  117. }
  118. if (sc_chan->idx == 0) {
  119. hdr = msg;
  120. sc_ipc->rx_size = hdr->size;
  121. dev_dbg(sc_ipc->dev, "msg rx size %u\n", sc_ipc->rx_size);
  122. if (sc_ipc->rx_size > 4)
  123. dev_warn(sc_ipc->dev, "RPC does not support receiving over 4 words: %u\n",
  124. sc_ipc->rx_size);
  125. }
  126. sc_ipc->msg[sc_chan->idx] = *data;
  127. sc_ipc->count++;
  128. dev_dbg(sc_ipc->dev, "mu %u msg %u 0x%x\n", sc_chan->idx,
  129. sc_ipc->count, *data);
  130. if ((sc_ipc->rx_size != 0) && (sc_ipc->count == sc_ipc->rx_size))
  131. complete(&sc_ipc->done);
  132. }
  133. static int imx_scu_ipc_write(struct imx_sc_ipc *sc_ipc, void *msg)
  134. {
  135. struct imx_sc_rpc_msg hdr = *(struct imx_sc_rpc_msg *)msg;
  136. struct imx_sc_chan *sc_chan;
  137. u32 *data = msg;
  138. int ret;
  139. int size;
  140. int i;
  141. /* Check size */
  142. if (hdr.size > IMX_SC_RPC_MAX_MSG)
  143. return -EINVAL;
  144. dev_dbg(sc_ipc->dev, "RPC SVC %u FUNC %u SIZE %u\n", hdr.svc,
  145. hdr.func, hdr.size);
  146. size = sc_ipc->fast_ipc ? 1 : hdr.size;
  147. for (i = 0; i < size; i++) {
  148. sc_chan = &sc_ipc->chans[i % 4];
  149. /*
  150. * SCU requires that all messages words are written
  151. * sequentially but linux MU driver implements multiple
  152. * independent channels for each register so ordering between
  153. * different channels must be ensured by SCU API interface.
  154. *
  155. * Wait for tx_done before every send to ensure that no
  156. * queueing happens at the mailbox channel level.
  157. */
  158. if (!sc_ipc->fast_ipc) {
  159. wait_for_completion(&sc_chan->tx_done);
  160. reinit_completion(&sc_chan->tx_done);
  161. }
  162. ret = mbox_send_message(sc_chan->ch, &data[i]);
  163. if (ret < 0)
  164. return ret;
  165. }
  166. return 0;
  167. }
  168. /*
  169. * RPC command/response
  170. */
  171. int imx_scu_call_rpc(struct imx_sc_ipc *sc_ipc, void *msg, bool have_resp)
  172. {
  173. uint8_t saved_svc, saved_func;
  174. struct imx_sc_rpc_msg *hdr;
  175. int ret;
  176. if (WARN_ON(!sc_ipc || !msg))
  177. return -EINVAL;
  178. mutex_lock(&sc_ipc->lock);
  179. reinit_completion(&sc_ipc->done);
  180. if (have_resp) {
  181. sc_ipc->msg = msg;
  182. saved_svc = ((struct imx_sc_rpc_msg *)msg)->svc;
  183. saved_func = ((struct imx_sc_rpc_msg *)msg)->func;
  184. }
  185. sc_ipc->count = 0;
  186. ret = imx_scu_ipc_write(sc_ipc, msg);
  187. if (ret < 0) {
  188. dev_err(sc_ipc->dev, "RPC send msg failed: %d\n", ret);
  189. goto out;
  190. }
  191. if (have_resp) {
  192. if (!wait_for_completion_timeout(&sc_ipc->done,
  193. MAX_RX_TIMEOUT)) {
  194. dev_err(sc_ipc->dev, "RPC send msg timeout\n");
  195. mutex_unlock(&sc_ipc->lock);
  196. return -ETIMEDOUT;
  197. }
  198. /* response status is stored in hdr->func field */
  199. hdr = msg;
  200. ret = hdr->func;
  201. /*
  202. * Some special SCU firmware APIs do NOT have return value
  203. * in hdr->func, but they do have response data, those special
  204. * APIs are defined as void function in SCU firmware, so they
  205. * should be treated as return success always.
  206. */
  207. if ((saved_svc == IMX_SC_RPC_SVC_MISC) &&
  208. (saved_func == IMX_SC_MISC_FUNC_UNIQUE_ID ||
  209. saved_func == IMX_SC_MISC_FUNC_GET_BUTTON_STATUS))
  210. ret = 0;
  211. }
  212. out:
  213. sc_ipc->msg = NULL;
  214. mutex_unlock(&sc_ipc->lock);
  215. dev_dbg(sc_ipc->dev, "RPC SVC done\n");
  216. return imx_sc_to_linux_errno(ret);
  217. }
  218. EXPORT_SYMBOL(imx_scu_call_rpc);
  219. static int imx_scu_probe(struct platform_device *pdev)
  220. {
  221. struct device *dev = &pdev->dev;
  222. struct imx_sc_ipc *sc_ipc;
  223. struct imx_sc_chan *sc_chan;
  224. struct mbox_client *cl;
  225. char *chan_name;
  226. struct of_phandle_args args;
  227. int num_channel;
  228. int ret;
  229. int i;
  230. sc_ipc = devm_kzalloc(dev, sizeof(*sc_ipc), GFP_KERNEL);
  231. if (!sc_ipc)
  232. return -ENOMEM;
  233. ret = of_parse_phandle_with_args(pdev->dev.of_node, "mboxes",
  234. "#mbox-cells", 0, &args);
  235. if (ret)
  236. return ret;
  237. sc_ipc->fast_ipc = of_device_is_compatible(args.np, "fsl,imx8-mu-scu");
  238. num_channel = sc_ipc->fast_ipc ? 2 : SCU_MU_CHAN_NUM;
  239. for (i = 0; i < num_channel; i++) {
  240. if (i < num_channel / 2)
  241. chan_name = kasprintf(GFP_KERNEL, "tx%d", i);
  242. else
  243. chan_name = kasprintf(GFP_KERNEL, "rx%d",
  244. i - num_channel / 2);
  245. if (!chan_name)
  246. return -ENOMEM;
  247. sc_chan = &sc_ipc->chans[i];
  248. cl = &sc_chan->cl;
  249. cl->dev = dev;
  250. cl->tx_block = false;
  251. cl->knows_txdone = true;
  252. cl->rx_callback = imx_scu_rx_callback;
  253. if (!sc_ipc->fast_ipc) {
  254. /* Initial tx_done completion as "done" */
  255. cl->tx_done = imx_scu_tx_done;
  256. init_completion(&sc_chan->tx_done);
  257. complete(&sc_chan->tx_done);
  258. }
  259. sc_chan->sc_ipc = sc_ipc;
  260. sc_chan->idx = i % (num_channel / 2);
  261. sc_chan->ch = mbox_request_channel_byname(cl, chan_name);
  262. if (IS_ERR(sc_chan->ch)) {
  263. ret = PTR_ERR(sc_chan->ch);
  264. if (ret != -EPROBE_DEFER)
  265. dev_err(dev, "Failed to request mbox chan %s ret %d\n",
  266. chan_name, ret);
  267. kfree(chan_name);
  268. return ret;
  269. }
  270. dev_dbg(dev, "request mbox chan %s\n", chan_name);
  271. /* chan_name is not used anymore by framework */
  272. kfree(chan_name);
  273. }
  274. sc_ipc->dev = dev;
  275. mutex_init(&sc_ipc->lock);
  276. init_completion(&sc_ipc->done);
  277. imx_sc_ipc_handle = sc_ipc;
  278. ret = imx_scu_soc_init(dev);
  279. if (ret)
  280. dev_warn(dev, "failed to initialize SoC info: %d\n", ret);
  281. ret = imx_scu_enable_general_irq_channel(dev);
  282. if (ret)
  283. dev_warn(dev,
  284. "failed to enable general irq channel: %d\n", ret);
  285. dev_info(dev, "NXP i.MX SCU Initialized\n");
  286. return devm_of_platform_populate(dev);
  287. }
  288. static const struct of_device_id imx_scu_match[] = {
  289. { .compatible = "fsl,imx-scu", },
  290. { /* Sentinel */ }
  291. };
  292. static struct platform_driver imx_scu_driver = {
  293. .driver = {
  294. .name = "imx-scu",
  295. .of_match_table = imx_scu_match,
  296. },
  297. .probe = imx_scu_probe,
  298. };
  299. builtin_platform_driver(imx_scu_driver);
  300. MODULE_AUTHOR("Dong Aisheng <[email protected]>");
  301. MODULE_DESCRIPTION("IMX SCU firmware protocol driver");
  302. MODULE_LICENSE("GPL v2");