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