gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / firmware / imx / imx-scu.c
blobf71eaa5bf52d411c750aa359aa13f4a00d88130c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2018 NXP
4 * Author: Dong Aisheng <aisheng.dong@nxp.com>
6 * Implementation of the SCU IPC functions using MUs (client side).
8 */
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>
23 #define SCU_MU_CHAN_NUM 8
24 #define MAX_RX_TIMEOUT (msecs_to_jiffies(30))
26 struct imx_sc_chan {
27 struct imx_sc_ipc *sc_ipc;
29 struct mbox_client cl;
30 struct mbox_chan *ch;
31 int idx;
32 struct completion tx_done;
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;
42 /* temporarily store the SCU msg */
43 u32 *msg;
44 u8 rx_size;
45 u8 count;
49 * This type is used to indicate error response for most functions.
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
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 */
82 static struct imx_sc_ipc *imx_sc_ipc_handle;
84 static inline int imx_sc_to_linux_errno(int errno)
86 if (errno >= IMX_SC_ERR_NONE && errno < IMX_SC_ERR_LAST)
87 return imx_sc_linux_errmap[errno];
88 return -EIO;
92 * Get the default handle used by SCU
94 int imx_scu_get_handle(struct imx_sc_ipc **ipc)
96 if (!imx_sc_ipc_handle)
97 return -EPROBE_DEFER;
99 *ipc = imx_sc_ipc_handle;
100 return 0;
102 EXPORT_SYMBOL(imx_scu_get_handle);
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)
107 struct imx_sc_chan *sc_chan = container_of(cl, struct imx_sc_chan, cl);
109 complete(&sc_chan->tx_done);
112 static void imx_scu_rx_callback(struct mbox_client *c, void *msg)
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;
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;
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);
134 sc_ipc->msg[sc_chan->idx] = *data;
135 sc_ipc->count++;
137 dev_dbg(sc_ipc->dev, "mu %u msg %u 0x%x\n", sc_chan->idx,
138 sc_ipc->count, *data);
140 if ((sc_ipc->rx_size != 0) && (sc_ipc->count == sc_ipc->rx_size))
141 complete(&sc_ipc->done);
144 static int imx_scu_ipc_write(struct imx_sc_ipc *sc_ipc, void *msg)
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;
152 /* Check size */
153 if (hdr->size > IMX_SC_RPC_MAX_MSG)
154 return -EINVAL;
156 dev_dbg(sc_ipc->dev, "RPC SVC %u FUNC %u SIZE %u\n", hdr->svc,
157 hdr->func, hdr->size);
159 for (i = 0; i < hdr->size; i++) {
160 sc_chan = &sc_ipc->chans[i % 4];
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.
168 * Wait for tx_done before every send to ensure that no
169 * queueing happens at the mailbox channel level.
171 wait_for_completion(&sc_chan->tx_done);
172 reinit_completion(&sc_chan->tx_done);
174 ret = mbox_send_message(sc_chan->ch, &data[i]);
175 if (ret < 0)
176 return ret;
179 return 0;
183 * RPC command/response
185 int imx_scu_call_rpc(struct imx_sc_ipc *sc_ipc, void *msg, bool have_resp)
187 uint8_t saved_svc, saved_func;
188 struct imx_sc_rpc_msg *hdr;
189 int ret;
191 if (WARN_ON(!sc_ipc || !msg))
192 return -EINVAL;
194 mutex_lock(&sc_ipc->lock);
195 reinit_completion(&sc_ipc->done);
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;
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;
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;
217 /* response status is stored in hdr->func field */
218 hdr = msg;
219 ret = hdr->func;
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.
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;
232 out:
233 sc_ipc->msg = NULL;
234 mutex_unlock(&sc_ipc->lock);
236 dev_dbg(sc_ipc->dev, "RPC SVC done\n");
238 return imx_sc_to_linux_errno(ret);
240 EXPORT_SYMBOL(imx_scu_call_rpc);
242 static int imx_scu_probe(struct platform_device *pdev)
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;
252 sc_ipc = devm_kzalloc(dev, sizeof(*sc_ipc), GFP_KERNEL);
253 if (!sc_ipc)
254 return -ENOMEM;
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);
262 if (!chan_name)
263 return -ENOMEM;
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;
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);
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;
288 dev_dbg(dev, "request mbox chan %s\n", chan_name);
289 /* chan_name is not used anymore by framework */
290 kfree(chan_name);
293 sc_ipc->dev = dev;
294 mutex_init(&sc_ipc->lock);
295 init_completion(&sc_ipc->done);
297 imx_sc_ipc_handle = sc_ipc;
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);
304 dev_info(dev, "NXP i.MX SCU Initialized\n");
306 return devm_of_platform_populate(dev);
309 static const struct of_device_id imx_scu_match[] = {
310 { .compatible = "fsl,imx-scu", },
311 { /* Sentinel */ }
314 static struct platform_driver imx_scu_driver = {
315 .driver = {
316 .name = "imx-scu",
317 .of_match_table = imx_scu_match,
319 .probe = imx_scu_probe,
321 builtin_platform_driver(imx_scu_driver);
323 MODULE_AUTHOR("Dong Aisheng <aisheng.dong@nxp.com>");
324 MODULE_DESCRIPTION("IMX SCU firmware protocol driver");
325 MODULE_LICENSE("GPL v2");