1edbee095SDong Aisheng // SPDX-License-Identifier: GPL-2.0+
2edbee095SDong Aisheng /*
3edbee095SDong Aisheng * Copyright 2018 NXP
4edbee095SDong Aisheng * Author: Dong Aisheng <aisheng.dong@nxp.com>
5edbee095SDong Aisheng *
6edbee095SDong Aisheng * Implementation of the SCU IPC functions using MUs (client side).
7edbee095SDong Aisheng *
8edbee095SDong Aisheng */
9edbee095SDong Aisheng
10edbee095SDong Aisheng #include <linux/err.h>
11edbee095SDong Aisheng #include <linux/firmware/imx/ipc.h>
12851826c7SAnson Huang #include <linux/firmware/imx/sci.h>
13edbee095SDong Aisheng #include <linux/interrupt.h>
14edbee095SDong Aisheng #include <linux/irq.h>
15edbee095SDong Aisheng #include <linux/kernel.h>
16edbee095SDong Aisheng #include <linux/mailbox_client.h>
17edbee095SDong Aisheng #include <linux/module.h>
18edbee095SDong Aisheng #include <linux/mutex.h>
19*5b45759cSRob Herring #include <linux/of.h>
20edbee095SDong Aisheng #include <linux/of_platform.h>
21edbee095SDong Aisheng #include <linux/platform_device.h>
22edbee095SDong Aisheng
23edbee095SDong Aisheng #define SCU_MU_CHAN_NUM 8
244b9ccf04SDong Aisheng #define MAX_RX_TIMEOUT (msecs_to_jiffies(3000))
25edbee095SDong Aisheng
26edbee095SDong Aisheng struct imx_sc_chan {
27edbee095SDong Aisheng struct imx_sc_ipc *sc_ipc;
28edbee095SDong Aisheng
29edbee095SDong Aisheng struct mbox_client cl;
30edbee095SDong Aisheng struct mbox_chan *ch;
31edbee095SDong Aisheng int idx;
3226d0fba2SLeonard Crestez struct completion tx_done;
33edbee095SDong Aisheng };
34edbee095SDong Aisheng
35edbee095SDong Aisheng struct imx_sc_ipc {
36edbee095SDong Aisheng /* SCU uses 4 Tx and 4 Rx channels */
37edbee095SDong Aisheng struct imx_sc_chan chans[SCU_MU_CHAN_NUM];
38edbee095SDong Aisheng struct device *dev;
39edbee095SDong Aisheng struct mutex lock;
40edbee095SDong Aisheng struct completion done;
41f25a066dSPeng Fan bool fast_ipc;
42edbee095SDong Aisheng
43edbee095SDong Aisheng /* temporarily store the SCU msg */
44edbee095SDong Aisheng u32 *msg;
45edbee095SDong Aisheng u8 rx_size;
46edbee095SDong Aisheng u8 count;
47edbee095SDong Aisheng };
48edbee095SDong Aisheng
49edbee095SDong Aisheng /*
50edbee095SDong Aisheng * This type is used to indicate error response for most functions.
51edbee095SDong Aisheng */
52edbee095SDong Aisheng enum imx_sc_error_codes {
53edbee095SDong Aisheng IMX_SC_ERR_NONE = 0, /* Success */
54edbee095SDong Aisheng IMX_SC_ERR_VERSION = 1, /* Incompatible API version */
55edbee095SDong Aisheng IMX_SC_ERR_CONFIG = 2, /* Configuration error */
56edbee095SDong Aisheng IMX_SC_ERR_PARM = 3, /* Bad parameter */
57edbee095SDong Aisheng IMX_SC_ERR_NOACCESS = 4, /* Permission error (no access) */
58edbee095SDong Aisheng IMX_SC_ERR_LOCKED = 5, /* Permission error (locked) */
59edbee095SDong Aisheng IMX_SC_ERR_UNAVAILABLE = 6, /* Unavailable (out of resources) */
60edbee095SDong Aisheng IMX_SC_ERR_NOTFOUND = 7, /* Not found */
61edbee095SDong Aisheng IMX_SC_ERR_NOPOWER = 8, /* No power */
62edbee095SDong Aisheng IMX_SC_ERR_IPC = 9, /* Generic IPC error */
63edbee095SDong Aisheng IMX_SC_ERR_BUSY = 10, /* Resource is currently busy/active */
64edbee095SDong Aisheng IMX_SC_ERR_FAIL = 11, /* General I/O failure */
65edbee095SDong Aisheng IMX_SC_ERR_LAST
66edbee095SDong Aisheng };
67edbee095SDong Aisheng
68edbee095SDong Aisheng static int imx_sc_linux_errmap[IMX_SC_ERR_LAST] = {
69edbee095SDong Aisheng 0, /* IMX_SC_ERR_NONE */
70edbee095SDong Aisheng -EINVAL, /* IMX_SC_ERR_VERSION */
71edbee095SDong Aisheng -EINVAL, /* IMX_SC_ERR_CONFIG */
72edbee095SDong Aisheng -EINVAL, /* IMX_SC_ERR_PARM */
73edbee095SDong Aisheng -EACCES, /* IMX_SC_ERR_NOACCESS */
74edbee095SDong Aisheng -EACCES, /* IMX_SC_ERR_LOCKED */
75edbee095SDong Aisheng -ERANGE, /* IMX_SC_ERR_UNAVAILABLE */
76edbee095SDong Aisheng -EEXIST, /* IMX_SC_ERR_NOTFOUND */
77edbee095SDong Aisheng -EPERM, /* IMX_SC_ERR_NOPOWER */
78edbee095SDong Aisheng -EPIPE, /* IMX_SC_ERR_IPC */
79edbee095SDong Aisheng -EBUSY, /* IMX_SC_ERR_BUSY */
80edbee095SDong Aisheng -EIO, /* IMX_SC_ERR_FAIL */
81edbee095SDong Aisheng };
82edbee095SDong Aisheng
83edbee095SDong Aisheng static struct imx_sc_ipc *imx_sc_ipc_handle;
84edbee095SDong Aisheng
imx_sc_to_linux_errno(int errno)85edbee095SDong Aisheng static inline int imx_sc_to_linux_errno(int errno)
86edbee095SDong Aisheng {
87edbee095SDong Aisheng if (errno >= IMX_SC_ERR_NONE && errno < IMX_SC_ERR_LAST)
88edbee095SDong Aisheng return imx_sc_linux_errmap[errno];
89edbee095SDong Aisheng return -EIO;
90edbee095SDong Aisheng }
91edbee095SDong Aisheng
92edbee095SDong Aisheng /*
93edbee095SDong Aisheng * Get the default handle used by SCU
94edbee095SDong Aisheng */
imx_scu_get_handle(struct imx_sc_ipc ** ipc)95edbee095SDong Aisheng int imx_scu_get_handle(struct imx_sc_ipc **ipc)
96edbee095SDong Aisheng {
97edbee095SDong Aisheng if (!imx_sc_ipc_handle)
98edbee095SDong Aisheng return -EPROBE_DEFER;
99edbee095SDong Aisheng
100edbee095SDong Aisheng *ipc = imx_sc_ipc_handle;
101edbee095SDong Aisheng return 0;
102edbee095SDong Aisheng }
103edbee095SDong Aisheng EXPORT_SYMBOL(imx_scu_get_handle);
104edbee095SDong Aisheng
10526d0fba2SLeonard Crestez /* Callback called when the word of a message is ack-ed, eg read by SCU */
imx_scu_tx_done(struct mbox_client * cl,void * mssg,int r)10626d0fba2SLeonard Crestez static void imx_scu_tx_done(struct mbox_client *cl, void *mssg, int r)
10726d0fba2SLeonard Crestez {
10826d0fba2SLeonard Crestez struct imx_sc_chan *sc_chan = container_of(cl, struct imx_sc_chan, cl);
10926d0fba2SLeonard Crestez
11026d0fba2SLeonard Crestez complete(&sc_chan->tx_done);
11126d0fba2SLeonard Crestez }
11226d0fba2SLeonard Crestez
imx_scu_rx_callback(struct mbox_client * c,void * msg)113edbee095SDong Aisheng static void imx_scu_rx_callback(struct mbox_client *c, void *msg)
114edbee095SDong Aisheng {
115edbee095SDong Aisheng struct imx_sc_chan *sc_chan = container_of(c, struct imx_sc_chan, cl);
116edbee095SDong Aisheng struct imx_sc_ipc *sc_ipc = sc_chan->sc_ipc;
117edbee095SDong Aisheng struct imx_sc_rpc_msg *hdr;
118edbee095SDong Aisheng u32 *data = msg;
119f25a066dSPeng Fan int i;
120edbee095SDong Aisheng
121cf0fd404SLeonard Crestez if (!sc_ipc->msg) {
122cf0fd404SLeonard Crestez dev_warn(sc_ipc->dev, "unexpected rx idx %d 0x%08x, ignore!\n",
123cf0fd404SLeonard Crestez sc_chan->idx, *data);
124cf0fd404SLeonard Crestez return;
125cf0fd404SLeonard Crestez }
126cf0fd404SLeonard Crestez
127f25a066dSPeng Fan if (sc_ipc->fast_ipc) {
128f25a066dSPeng Fan hdr = msg;
129f25a066dSPeng Fan sc_ipc->rx_size = hdr->size;
130f25a066dSPeng Fan sc_ipc->msg[0] = *data++;
131f25a066dSPeng Fan
132f25a066dSPeng Fan for (i = 1; i < sc_ipc->rx_size; i++)
133f25a066dSPeng Fan sc_ipc->msg[i] = *data++;
134f25a066dSPeng Fan
135f25a066dSPeng Fan complete(&sc_ipc->done);
136f25a066dSPeng Fan
137f25a066dSPeng Fan return;
138f25a066dSPeng Fan }
139f25a066dSPeng Fan
140edbee095SDong Aisheng if (sc_chan->idx == 0) {
141edbee095SDong Aisheng hdr = msg;
142edbee095SDong Aisheng sc_ipc->rx_size = hdr->size;
143edbee095SDong Aisheng dev_dbg(sc_ipc->dev, "msg rx size %u\n", sc_ipc->rx_size);
144edbee095SDong Aisheng if (sc_ipc->rx_size > 4)
145edbee095SDong Aisheng dev_warn(sc_ipc->dev, "RPC does not support receiving over 4 words: %u\n",
146edbee095SDong Aisheng sc_ipc->rx_size);
147edbee095SDong Aisheng }
148edbee095SDong Aisheng
149edbee095SDong Aisheng sc_ipc->msg[sc_chan->idx] = *data;
150edbee095SDong Aisheng sc_ipc->count++;
151edbee095SDong Aisheng
152edbee095SDong Aisheng dev_dbg(sc_ipc->dev, "mu %u msg %u 0x%x\n", sc_chan->idx,
153edbee095SDong Aisheng sc_ipc->count, *data);
154edbee095SDong Aisheng
155edbee095SDong Aisheng if ((sc_ipc->rx_size != 0) && (sc_ipc->count == sc_ipc->rx_size))
156edbee095SDong Aisheng complete(&sc_ipc->done);
157edbee095SDong Aisheng }
158edbee095SDong Aisheng
imx_scu_ipc_write(struct imx_sc_ipc * sc_ipc,void * msg)159edbee095SDong Aisheng static int imx_scu_ipc_write(struct imx_sc_ipc *sc_ipc, void *msg)
160edbee095SDong Aisheng {
161f5f27b79SFranck LENORMAND struct imx_sc_rpc_msg hdr = *(struct imx_sc_rpc_msg *)msg;
162edbee095SDong Aisheng struct imx_sc_chan *sc_chan;
163edbee095SDong Aisheng u32 *data = msg;
164edbee095SDong Aisheng int ret;
165f25a066dSPeng Fan int size;
166edbee095SDong Aisheng int i;
167edbee095SDong Aisheng
168edbee095SDong Aisheng /* Check size */
169f5f27b79SFranck LENORMAND if (hdr.size > IMX_SC_RPC_MAX_MSG)
170edbee095SDong Aisheng return -EINVAL;
171edbee095SDong Aisheng
172f5f27b79SFranck LENORMAND dev_dbg(sc_ipc->dev, "RPC SVC %u FUNC %u SIZE %u\n", hdr.svc,
173f5f27b79SFranck LENORMAND hdr.func, hdr.size);
174edbee095SDong Aisheng
175f5f27b79SFranck LENORMAND size = sc_ipc->fast_ipc ? 1 : hdr.size;
176f25a066dSPeng Fan for (i = 0; i < size; i++) {
177edbee095SDong Aisheng sc_chan = &sc_ipc->chans[i % 4];
17826d0fba2SLeonard Crestez
17926d0fba2SLeonard Crestez /*
18026d0fba2SLeonard Crestez * SCU requires that all messages words are written
18126d0fba2SLeonard Crestez * sequentially but linux MU driver implements multiple
18226d0fba2SLeonard Crestez * independent channels for each register so ordering between
18326d0fba2SLeonard Crestez * different channels must be ensured by SCU API interface.
18426d0fba2SLeonard Crestez *
18526d0fba2SLeonard Crestez * Wait for tx_done before every send to ensure that no
18626d0fba2SLeonard Crestez * queueing happens at the mailbox channel level.
18726d0fba2SLeonard Crestez */
188f25a066dSPeng Fan if (!sc_ipc->fast_ipc) {
18926d0fba2SLeonard Crestez wait_for_completion(&sc_chan->tx_done);
19026d0fba2SLeonard Crestez reinit_completion(&sc_chan->tx_done);
191f25a066dSPeng Fan }
19226d0fba2SLeonard Crestez
193edbee095SDong Aisheng ret = mbox_send_message(sc_chan->ch, &data[i]);
194edbee095SDong Aisheng if (ret < 0)
195edbee095SDong Aisheng return ret;
196edbee095SDong Aisheng }
197edbee095SDong Aisheng
198edbee095SDong Aisheng return 0;
199edbee095SDong Aisheng }
200edbee095SDong Aisheng
201edbee095SDong Aisheng /*
202edbee095SDong Aisheng * RPC command/response
203edbee095SDong Aisheng */
imx_scu_call_rpc(struct imx_sc_ipc * sc_ipc,void * msg,bool have_resp)204edbee095SDong Aisheng int imx_scu_call_rpc(struct imx_sc_ipc *sc_ipc, void *msg, bool have_resp)
205edbee095SDong Aisheng {
20651f5afabSAnson Huang uint8_t saved_svc, saved_func;
207edbee095SDong Aisheng struct imx_sc_rpc_msg *hdr;
208edbee095SDong Aisheng int ret;
209edbee095SDong Aisheng
210edbee095SDong Aisheng if (WARN_ON(!sc_ipc || !msg))
211edbee095SDong Aisheng return -EINVAL;
212edbee095SDong Aisheng
213edbee095SDong Aisheng mutex_lock(&sc_ipc->lock);
214edbee095SDong Aisheng reinit_completion(&sc_ipc->done);
215edbee095SDong Aisheng
21651f5afabSAnson Huang if (have_resp) {
217edbee095SDong Aisheng sc_ipc->msg = msg;
21851f5afabSAnson Huang saved_svc = ((struct imx_sc_rpc_msg *)msg)->svc;
21951f5afabSAnson Huang saved_func = ((struct imx_sc_rpc_msg *)msg)->func;
22051f5afabSAnson Huang }
221edbee095SDong Aisheng sc_ipc->count = 0;
222edbee095SDong Aisheng ret = imx_scu_ipc_write(sc_ipc, msg);
223edbee095SDong Aisheng if (ret < 0) {
224edbee095SDong Aisheng dev_err(sc_ipc->dev, "RPC send msg failed: %d\n", ret);
225edbee095SDong Aisheng goto out;
226edbee095SDong Aisheng }
227edbee095SDong Aisheng
228edbee095SDong Aisheng if (have_resp) {
229edbee095SDong Aisheng if (!wait_for_completion_timeout(&sc_ipc->done,
230edbee095SDong Aisheng MAX_RX_TIMEOUT)) {
231edbee095SDong Aisheng dev_err(sc_ipc->dev, "RPC send msg timeout\n");
232edbee095SDong Aisheng mutex_unlock(&sc_ipc->lock);
233edbee095SDong Aisheng return -ETIMEDOUT;
234edbee095SDong Aisheng }
235edbee095SDong Aisheng
236edbee095SDong Aisheng /* response status is stored in hdr->func field */
237edbee095SDong Aisheng hdr = msg;
238edbee095SDong Aisheng ret = hdr->func;
23951f5afabSAnson Huang /*
24051f5afabSAnson Huang * Some special SCU firmware APIs do NOT have return value
24151f5afabSAnson Huang * in hdr->func, but they do have response data, those special
24251f5afabSAnson Huang * APIs are defined as void function in SCU firmware, so they
24351f5afabSAnson Huang * should be treated as return success always.
24451f5afabSAnson Huang */
24551f5afabSAnson Huang if ((saved_svc == IMX_SC_RPC_SVC_MISC) &&
24651f5afabSAnson Huang (saved_func == IMX_SC_MISC_FUNC_UNIQUE_ID ||
24751f5afabSAnson Huang saved_func == IMX_SC_MISC_FUNC_GET_BUTTON_STATUS))
24851f5afabSAnson Huang ret = 0;
249edbee095SDong Aisheng }
250edbee095SDong Aisheng
251edbee095SDong Aisheng out:
252cf0fd404SLeonard Crestez sc_ipc->msg = NULL;
253edbee095SDong Aisheng mutex_unlock(&sc_ipc->lock);
254edbee095SDong Aisheng
255edbee095SDong Aisheng dev_dbg(sc_ipc->dev, "RPC SVC done\n");
256edbee095SDong Aisheng
257edbee095SDong Aisheng return imx_sc_to_linux_errno(ret);
258edbee095SDong Aisheng }
259edbee095SDong Aisheng EXPORT_SYMBOL(imx_scu_call_rpc);
260edbee095SDong Aisheng
imx_scu_probe(struct platform_device * pdev)261edbee095SDong Aisheng static int imx_scu_probe(struct platform_device *pdev)
262edbee095SDong Aisheng {
263edbee095SDong Aisheng struct device *dev = &pdev->dev;
264edbee095SDong Aisheng struct imx_sc_ipc *sc_ipc;
265edbee095SDong Aisheng struct imx_sc_chan *sc_chan;
266edbee095SDong Aisheng struct mbox_client *cl;
267edbee095SDong Aisheng char *chan_name;
268f25a066dSPeng Fan struct of_phandle_args args;
269f25a066dSPeng Fan int num_channel;
270edbee095SDong Aisheng int ret;
271edbee095SDong Aisheng int i;
272edbee095SDong Aisheng
273edbee095SDong Aisheng sc_ipc = devm_kzalloc(dev, sizeof(*sc_ipc), GFP_KERNEL);
274edbee095SDong Aisheng if (!sc_ipc)
275edbee095SDong Aisheng return -ENOMEM;
276edbee095SDong Aisheng
277f25a066dSPeng Fan ret = of_parse_phandle_with_args(pdev->dev.of_node, "mboxes",
278f25a066dSPeng Fan "#mbox-cells", 0, &args);
279f25a066dSPeng Fan if (ret)
280f25a066dSPeng Fan return ret;
281f25a066dSPeng Fan
282f25a066dSPeng Fan sc_ipc->fast_ipc = of_device_is_compatible(args.np, "fsl,imx8-mu-scu");
283f25a066dSPeng Fan
284f25a066dSPeng Fan num_channel = sc_ipc->fast_ipc ? 2 : SCU_MU_CHAN_NUM;
285f25a066dSPeng Fan for (i = 0; i < num_channel; i++) {
286f25a066dSPeng Fan if (i < num_channel / 2)
287edbee095SDong Aisheng chan_name = kasprintf(GFP_KERNEL, "tx%d", i);
288edbee095SDong Aisheng else
289f25a066dSPeng Fan chan_name = kasprintf(GFP_KERNEL, "rx%d",
290f25a066dSPeng Fan i - num_channel / 2);
291edbee095SDong Aisheng
292edbee095SDong Aisheng if (!chan_name)
293edbee095SDong Aisheng return -ENOMEM;
294edbee095SDong Aisheng
295edbee095SDong Aisheng sc_chan = &sc_ipc->chans[i];
296edbee095SDong Aisheng cl = &sc_chan->cl;
297edbee095SDong Aisheng cl->dev = dev;
298edbee095SDong Aisheng cl->tx_block = false;
299edbee095SDong Aisheng cl->knows_txdone = true;
300edbee095SDong Aisheng cl->rx_callback = imx_scu_rx_callback;
301edbee095SDong Aisheng
302f25a066dSPeng Fan if (!sc_ipc->fast_ipc) {
30326d0fba2SLeonard Crestez /* Initial tx_done completion as "done" */
30426d0fba2SLeonard Crestez cl->tx_done = imx_scu_tx_done;
30526d0fba2SLeonard Crestez init_completion(&sc_chan->tx_done);
30626d0fba2SLeonard Crestez complete(&sc_chan->tx_done);
307f25a066dSPeng Fan }
30826d0fba2SLeonard Crestez
309edbee095SDong Aisheng sc_chan->sc_ipc = sc_ipc;
310f25a066dSPeng Fan sc_chan->idx = i % (num_channel / 2);
311edbee095SDong Aisheng sc_chan->ch = mbox_request_channel_byname(cl, chan_name);
312edbee095SDong Aisheng if (IS_ERR(sc_chan->ch)) {
313edbee095SDong Aisheng ret = PTR_ERR(sc_chan->ch);
314189cfa84SAlexander Stein dev_err_probe(dev, ret, "Failed to request mbox chan %s\n",
315189cfa84SAlexander Stein chan_name);
31689f12d65SWei Yongjun kfree(chan_name);
317edbee095SDong Aisheng return ret;
318edbee095SDong Aisheng }
319edbee095SDong Aisheng
320edbee095SDong Aisheng dev_dbg(dev, "request mbox chan %s\n", chan_name);
321edbee095SDong Aisheng /* chan_name is not used anymore by framework */
322edbee095SDong Aisheng kfree(chan_name);
323edbee095SDong Aisheng }
324edbee095SDong Aisheng
325edbee095SDong Aisheng sc_ipc->dev = dev;
326edbee095SDong Aisheng mutex_init(&sc_ipc->lock);
327edbee095SDong Aisheng init_completion(&sc_ipc->done);
328edbee095SDong Aisheng
329edbee095SDong Aisheng imx_sc_ipc_handle = sc_ipc;
330edbee095SDong Aisheng
331a24015faSAnson Huang ret = imx_scu_soc_init(dev);
332a24015faSAnson Huang if (ret)
333a24015faSAnson Huang dev_warn(dev, "failed to initialize SoC info: %d\n", ret);
334a24015faSAnson Huang
335851826c7SAnson Huang ret = imx_scu_enable_general_irq_channel(dev);
336851826c7SAnson Huang if (ret)
337851826c7SAnson Huang dev_warn(dev,
338851826c7SAnson Huang "failed to enable general irq channel: %d\n", ret);
339851826c7SAnson Huang
340edbee095SDong Aisheng dev_info(dev, "NXP i.MX SCU Initialized\n");
341edbee095SDong Aisheng
342edbee095SDong Aisheng return devm_of_platform_populate(dev);
343edbee095SDong Aisheng }
344edbee095SDong Aisheng
345edbee095SDong Aisheng static const struct of_device_id imx_scu_match[] = {
346edbee095SDong Aisheng { .compatible = "fsl,imx-scu", },
347edbee095SDong Aisheng { /* Sentinel */ }
348edbee095SDong Aisheng };
349edbee095SDong Aisheng
350edbee095SDong Aisheng static struct platform_driver imx_scu_driver = {
351edbee095SDong Aisheng .driver = {
352edbee095SDong Aisheng .name = "imx-scu",
353edbee095SDong Aisheng .of_match_table = imx_scu_match,
354edbee095SDong Aisheng },
355edbee095SDong Aisheng .probe = imx_scu_probe,
356edbee095SDong Aisheng };
35722420dc7SDong Aisheng
imx_scu_driver_init(void)35822420dc7SDong Aisheng static int __init imx_scu_driver_init(void)
35922420dc7SDong Aisheng {
36022420dc7SDong Aisheng return platform_driver_register(&imx_scu_driver);
36122420dc7SDong Aisheng }
36222420dc7SDong Aisheng subsys_initcall_sync(imx_scu_driver_init);
363edbee095SDong Aisheng
364edbee095SDong Aisheng MODULE_AUTHOR("Dong Aisheng <aisheng.dong@nxp.com>");
365edbee095SDong Aisheng MODULE_DESCRIPTION("IMX SCU firmware protocol driver");
366edbee095SDong Aisheng MODULE_LICENSE("GPL v2");
367