imx-scu.c 9.0 KB

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