Skip to content

Commit edbee09

Browse files
Dong AishengShawn Guo
authored andcommitted
firmware: imx: add SCU firmware driver support
The System Controller Firmware (SCFW) is a low-level system function which runs on a dedicated Cortex-M core to provide power, clock, and resource management. It exists on some i.MX8 processors. e.g. i.MX8QM (QM, QP), and i.MX8QX (QXP, DX). This patch implements the SCU firmware IPC function and the common message sending API sc_call_rpc. Cc: Shawn Guo <shawnguo@kernel.org> Cc: Fabio Estevam <fabio.estevam@nxp.com> Cc: Jassi Brar <jassisinghbrar@gmail.com> Reviewed-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Dong Aisheng <aisheng.dong@nxp.com> Signed-off-by: Shawn Guo <shawnguo@kernel.org>
1 parent c6c2ee0 commit edbee09

File tree

8 files changed

+977
-0
lines changed

8 files changed

+977
-0
lines changed

drivers/firmware/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ config HAVE_ARM_SMCCC
289289
source "drivers/firmware/broadcom/Kconfig"
290290
source "drivers/firmware/google/Kconfig"
291291
source "drivers/firmware/efi/Kconfig"
292+
source "drivers/firmware/imx/Kconfig"
292293
source "drivers/firmware/meson/Kconfig"
293294
source "drivers/firmware/tegra/Kconfig"
294295

drivers/firmware/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ obj-y += meson/
3131
obj-$(CONFIG_GOOGLE_FIRMWARE) += google/
3232
obj-$(CONFIG_EFI) += efi/
3333
obj-$(CONFIG_UEFI_CPER) += efi/
34+
obj-y += imx/
3435
obj-y += tegra/

drivers/firmware/imx/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
config IMX_SCU
2+
bool "IMX SCU Protocol driver"
3+
depends on IMX_MBOX
4+
help
5+
The System Controller Firmware (SCFW) is a low-level system function
6+
which runs on a dedicated Cortex-M core to provide power, clock, and
7+
resource management. It exists on some i.MX8 processors. e.g. i.MX8QM
8+
(QM, QP), and i.MX8QX (QXP, DX).
9+
10+
This driver manages the IPC interface between host CPU and the
11+
SCU firmware running on M4.

drivers/firmware/imx/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
obj-$(CONFIG_IMX_SCU) += imx-scu.o

drivers/firmware/imx/imx-scu.c

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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/interrupt.h>
14+
#include <linux/irq.h>
15+
#include <linux/kernel.h>
16+
#include <linux/mailbox_client.h>
17+
#include <linux/module.h>
18+
#include <linux/mutex.h>
19+
#include <linux/of_platform.h>
20+
#include <linux/platform_device.h>
21+
22+
#define SCU_MU_CHAN_NUM 8
23+
#define MAX_RX_TIMEOUT (msecs_to_jiffies(30))
24+
25+
struct imx_sc_chan {
26+
struct imx_sc_ipc *sc_ipc;
27+
28+
struct mbox_client cl;
29+
struct mbox_chan *ch;
30+
int idx;
31+
};
32+
33+
struct imx_sc_ipc {
34+
/* SCU uses 4 Tx and 4 Rx channels */
35+
struct imx_sc_chan chans[SCU_MU_CHAN_NUM];
36+
struct device *dev;
37+
struct mutex lock;
38+
struct completion done;
39+
40+
/* temporarily store the SCU msg */
41+
u32 *msg;
42+
u8 rx_size;
43+
u8 count;
44+
};
45+
46+
/*
47+
* This type is used to indicate error response for most functions.
48+
*/
49+
enum imx_sc_error_codes {
50+
IMX_SC_ERR_NONE = 0, /* Success */
51+
IMX_SC_ERR_VERSION = 1, /* Incompatible API version */
52+
IMX_SC_ERR_CONFIG = 2, /* Configuration error */
53+
IMX_SC_ERR_PARM = 3, /* Bad parameter */
54+
IMX_SC_ERR_NOACCESS = 4, /* Permission error (no access) */
55+
IMX_SC_ERR_LOCKED = 5, /* Permission error (locked) */
56+
IMX_SC_ERR_UNAVAILABLE = 6, /* Unavailable (out of resources) */
57+
IMX_SC_ERR_NOTFOUND = 7, /* Not found */
58+
IMX_SC_ERR_NOPOWER = 8, /* No power */
59+
IMX_SC_ERR_IPC = 9, /* Generic IPC error */
60+
IMX_SC_ERR_BUSY = 10, /* Resource is currently busy/active */
61+
IMX_SC_ERR_FAIL = 11, /* General I/O failure */
62+
IMX_SC_ERR_LAST
63+
};
64+
65+
static int imx_sc_linux_errmap[IMX_SC_ERR_LAST] = {
66+
0, /* IMX_SC_ERR_NONE */
67+
-EINVAL, /* IMX_SC_ERR_VERSION */
68+
-EINVAL, /* IMX_SC_ERR_CONFIG */
69+
-EINVAL, /* IMX_SC_ERR_PARM */
70+
-EACCES, /* IMX_SC_ERR_NOACCESS */
71+
-EACCES, /* IMX_SC_ERR_LOCKED */
72+
-ERANGE, /* IMX_SC_ERR_UNAVAILABLE */
73+
-EEXIST, /* IMX_SC_ERR_NOTFOUND */
74+
-EPERM, /* IMX_SC_ERR_NOPOWER */
75+
-EPIPE, /* IMX_SC_ERR_IPC */
76+
-EBUSY, /* IMX_SC_ERR_BUSY */
77+
-EIO, /* IMX_SC_ERR_FAIL */
78+
};
79+
80+
static struct imx_sc_ipc *imx_sc_ipc_handle;
81+
82+
static inline int imx_sc_to_linux_errno(int errno)
83+
{
84+
if (errno >= IMX_SC_ERR_NONE && errno < IMX_SC_ERR_LAST)
85+
return imx_sc_linux_errmap[errno];
86+
return -EIO;
87+
}
88+
89+
/*
90+
* Get the default handle used by SCU
91+
*/
92+
int imx_scu_get_handle(struct imx_sc_ipc **ipc)
93+
{
94+
if (!imx_sc_ipc_handle)
95+
return -EPROBE_DEFER;
96+
97+
*ipc = imx_sc_ipc_handle;
98+
return 0;
99+
}
100+
EXPORT_SYMBOL(imx_scu_get_handle);
101+
102+
static void imx_scu_rx_callback(struct mbox_client *c, void *msg)
103+
{
104+
struct imx_sc_chan *sc_chan = container_of(c, struct imx_sc_chan, cl);
105+
struct imx_sc_ipc *sc_ipc = sc_chan->sc_ipc;
106+
struct imx_sc_rpc_msg *hdr;
107+
u32 *data = msg;
108+
109+
if (sc_chan->idx == 0) {
110+
hdr = msg;
111+
sc_ipc->rx_size = hdr->size;
112+
dev_dbg(sc_ipc->dev, "msg rx size %u\n", sc_ipc->rx_size);
113+
if (sc_ipc->rx_size > 4)
114+
dev_warn(sc_ipc->dev, "RPC does not support receiving over 4 words: %u\n",
115+
sc_ipc->rx_size);
116+
}
117+
118+
sc_ipc->msg[sc_chan->idx] = *data;
119+
sc_ipc->count++;
120+
121+
dev_dbg(sc_ipc->dev, "mu %u msg %u 0x%x\n", sc_chan->idx,
122+
sc_ipc->count, *data);
123+
124+
if ((sc_ipc->rx_size != 0) && (sc_ipc->count == sc_ipc->rx_size))
125+
complete(&sc_ipc->done);
126+
}
127+
128+
static int imx_scu_ipc_write(struct imx_sc_ipc *sc_ipc, void *msg)
129+
{
130+
struct imx_sc_rpc_msg *hdr = msg;
131+
struct imx_sc_chan *sc_chan;
132+
u32 *data = msg;
133+
int ret;
134+
int i;
135+
136+
/* Check size */
137+
if (hdr->size > IMX_SC_RPC_MAX_MSG)
138+
return -EINVAL;
139+
140+
dev_dbg(sc_ipc->dev, "RPC SVC %u FUNC %u SIZE %u\n", hdr->svc,
141+
hdr->func, hdr->size);
142+
143+
for (i = 0; i < hdr->size; i++) {
144+
sc_chan = &sc_ipc->chans[i % 4];
145+
ret = mbox_send_message(sc_chan->ch, &data[i]);
146+
if (ret < 0)
147+
return ret;
148+
}
149+
150+
return 0;
151+
}
152+
153+
/*
154+
* RPC command/response
155+
*/
156+
int imx_scu_call_rpc(struct imx_sc_ipc *sc_ipc, void *msg, bool have_resp)
157+
{
158+
struct imx_sc_rpc_msg *hdr;
159+
int ret;
160+
161+
if (WARN_ON(!sc_ipc || !msg))
162+
return -EINVAL;
163+
164+
mutex_lock(&sc_ipc->lock);
165+
reinit_completion(&sc_ipc->done);
166+
167+
sc_ipc->msg = msg;
168+
sc_ipc->count = 0;
169+
ret = imx_scu_ipc_write(sc_ipc, msg);
170+
if (ret < 0) {
171+
dev_err(sc_ipc->dev, "RPC send msg failed: %d\n", ret);
172+
goto out;
173+
}
174+
175+
if (have_resp) {
176+
if (!wait_for_completion_timeout(&sc_ipc->done,
177+
MAX_RX_TIMEOUT)) {
178+
dev_err(sc_ipc->dev, "RPC send msg timeout\n");
179+
mutex_unlock(&sc_ipc->lock);
180+
return -ETIMEDOUT;
181+
}
182+
183+
/* response status is stored in hdr->func field */
184+
hdr = msg;
185+
ret = hdr->func;
186+
}
187+
188+
out:
189+
mutex_unlock(&sc_ipc->lock);
190+
191+
dev_dbg(sc_ipc->dev, "RPC SVC done\n");
192+
193+
return imx_sc_to_linux_errno(ret);
194+
}
195+
EXPORT_SYMBOL(imx_scu_call_rpc);
196+
197+
static int imx_scu_probe(struct platform_device *pdev)
198+
{
199+
struct device *dev = &pdev->dev;
200+
struct imx_sc_ipc *sc_ipc;
201+
struct imx_sc_chan *sc_chan;
202+
struct mbox_client *cl;
203+
char *chan_name;
204+
int ret;
205+
int i;
206+
207+
sc_ipc = devm_kzalloc(dev, sizeof(*sc_ipc), GFP_KERNEL);
208+
if (!sc_ipc)
209+
return -ENOMEM;
210+
211+
for (i = 0; i < SCU_MU_CHAN_NUM; i++) {
212+
if (i < 4)
213+
chan_name = kasprintf(GFP_KERNEL, "tx%d", i);
214+
else
215+
chan_name = kasprintf(GFP_KERNEL, "rx%d", i - 4);
216+
217+
if (!chan_name)
218+
return -ENOMEM;
219+
220+
sc_chan = &sc_ipc->chans[i];
221+
cl = &sc_chan->cl;
222+
cl->dev = dev;
223+
cl->tx_block = false;
224+
cl->knows_txdone = true;
225+
cl->rx_callback = imx_scu_rx_callback;
226+
227+
sc_chan->sc_ipc = sc_ipc;
228+
sc_chan->idx = i % 4;
229+
sc_chan->ch = mbox_request_channel_byname(cl, chan_name);
230+
if (IS_ERR(sc_chan->ch)) {
231+
ret = PTR_ERR(sc_chan->ch);
232+
if (ret != -EPROBE_DEFER)
233+
dev_err(dev, "Failed to request mbox chan %s ret %d\n",
234+
chan_name, ret);
235+
return ret;
236+
}
237+
238+
dev_dbg(dev, "request mbox chan %s\n", chan_name);
239+
/* chan_name is not used anymore by framework */
240+
kfree(chan_name);
241+
}
242+
243+
sc_ipc->dev = dev;
244+
mutex_init(&sc_ipc->lock);
245+
init_completion(&sc_ipc->done);
246+
247+
imx_sc_ipc_handle = sc_ipc;
248+
249+
dev_info(dev, "NXP i.MX SCU Initialized\n");
250+
251+
return devm_of_platform_populate(dev);
252+
}
253+
254+
static const struct of_device_id imx_scu_match[] = {
255+
{ .compatible = "fsl,imx-scu", },
256+
{ /* Sentinel */ }
257+
};
258+
259+
static struct platform_driver imx_scu_driver = {
260+
.driver = {
261+
.name = "imx-scu",
262+
.of_match_table = imx_scu_match,
263+
},
264+
.probe = imx_scu_probe,
265+
};
266+
builtin_platform_driver(imx_scu_driver);
267+
268+
MODULE_AUTHOR("Dong Aisheng <aisheng.dong@nxp.com>");
269+
MODULE_DESCRIPTION("IMX SCU firmware protocol driver");
270+
MODULE_LICENSE("GPL v2");

include/linux/firmware/imx/ipc.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
/*
3+
* Copyright 2018 NXP
4+
*
5+
* Header file for the IPC implementation.
6+
*/
7+
8+
#ifndef _SC_IPC_H
9+
#define _SC_IPC_H
10+
11+
#include <linux/device.h>
12+
#include <linux/types.h>
13+
14+
#define IMX_SC_RPC_VERSION 1
15+
#define IMX_SC_RPC_MAX_MSG 8
16+
17+
struct imx_sc_ipc;
18+
19+
enum imx_sc_rpc_svc {
20+
IMX_SC_RPC_SVC_UNKNOWN = 0,
21+
IMX_SC_RPC_SVC_RETURN = 1,
22+
IMX_SC_RPC_SVC_PM = 2,
23+
IMX_SC_RPC_SVC_RM = 3,
24+
IMX_SC_RPC_SVC_TIMER = 5,
25+
IMX_SC_RPC_SVC_PAD = 6,
26+
IMX_SC_RPC_SVC_MISC = 7,
27+
IMX_SC_RPC_SVC_IRQ = 8,
28+
IMX_SC_RPC_SVC_ABORT = 9
29+
};
30+
31+
struct imx_sc_rpc_msg {
32+
uint8_t ver;
33+
uint8_t size;
34+
uint8_t svc;
35+
uint8_t func;
36+
};
37+
38+
/*
39+
* This is an function to send an RPC message over an IPC channel.
40+
* It is called by client-side SCFW API function shims.
41+
*
42+
* @param[in] ipc IPC handle
43+
* @param[in,out] msg handle to a message
44+
* @param[in] have_resp response flag
45+
*
46+
* If have_resp is true then this function waits for a response
47+
* and returns the result in msg.
48+
*/
49+
int imx_scu_call_rpc(struct imx_sc_ipc *ipc, void *msg, bool have_resp);
50+
51+
/*
52+
* This function gets the default ipc handle used by SCU
53+
*
54+
* @param[out] ipc sc ipc handle
55+
*
56+
* @return Returns an error code (0 = success, failed if < 0)
57+
*/
58+
int imx_scu_get_handle(struct imx_sc_ipc **ipc);
59+
#endif /* _SC_IPC_H */

include/linux/firmware/imx/sci.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
/*
3+
* Copyright (C) 2016 Freescale Semiconductor, Inc.
4+
* Copyright 2017~2018 NXP
5+
*
6+
* Header file containing the public System Controller Interface (SCI)
7+
* definitions.
8+
*/
9+
10+
#ifndef _SC_SCI_H
11+
#define _SC_SCI_H
12+
13+
#include <linux/firmware/imx/ipc.h>
14+
#include <linux/firmware/imx/types.h>
15+
16+
#endif /* _SC_SCI_H */

0 commit comments

Comments
 (0)