diff options
author | Arnd Bergmann <arnd@arndb.de> | 2018-10-10 07:35:29 -0400 |
---|---|---|
committer | Arnd Bergmann <arnd@arndb.de> | 2018-10-10 07:40:22 -0400 |
commit | b0a2cea5eb637d540ba86fd31dfd750f26ee0161 (patch) | |
tree | e1c55af1ff79a76f6c7cd71efc46f6ca9fa0fff4 /drivers | |
parent | 75bda3609f94a24a213fa56235bf369056565299 (diff) | |
parent | b912de514a8759ef558096d7f1c3a0000e0e37f0 (diff) |
Merge tag 'imx-drivers-4.20-2' of git://git.kernel.org/pub/scm/linux/kernel/git/shawnguo/linux into next/drivers
i.MX drivers change for 4.20, round 2:
- A series from Aisheng Dong to add SCU firmware driver for i.MX8
SoCs. It implements IPC mechanism based on mailbox for message
exchange between AP and SCU firmware, and a set of SCU IPC
service APIs used by clients like i.MX8 power domain and clock
drivers.
* tag 'imx-drivers-4.20-2' of git://git.kernel.org/pub/scm/linux/kernel/git/shawnguo/linux:
MAINTAINERS: imx: include drivers/firmware/imx path
firmware: imx: add misc svc support
firmware: imx: add SCU firmware driver support
dt-bindings: arm: fsl: add scu binding doc
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/firmware/Kconfig | 1 | ||||
-rw-r--r-- | drivers/firmware/Makefile | 1 | ||||
-rw-r--r-- | drivers/firmware/imx/Kconfig | 11 | ||||
-rw-r--r-- | drivers/firmware/imx/Makefile | 2 | ||||
-rw-r--r-- | drivers/firmware/imx/imx-scu.c | 270 | ||||
-rw-r--r-- | drivers/firmware/imx/misc.c | 99 |
6 files changed, 384 insertions, 0 deletions
diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig index 36344cba764e..7670e8dda829 100644 --- a/drivers/firmware/Kconfig +++ b/drivers/firmware/Kconfig | |||
@@ -289,6 +289,7 @@ config HAVE_ARM_SMCCC | |||
289 | source "drivers/firmware/broadcom/Kconfig" | 289 | source "drivers/firmware/broadcom/Kconfig" |
290 | source "drivers/firmware/google/Kconfig" | 290 | source "drivers/firmware/google/Kconfig" |
291 | source "drivers/firmware/efi/Kconfig" | 291 | source "drivers/firmware/efi/Kconfig" |
292 | source "drivers/firmware/imx/Kconfig" | ||
292 | source "drivers/firmware/meson/Kconfig" | 293 | source "drivers/firmware/meson/Kconfig" |
293 | source "drivers/firmware/tegra/Kconfig" | 294 | source "drivers/firmware/tegra/Kconfig" |
294 | source "drivers/firmware/xilinx/Kconfig" | 295 | source "drivers/firmware/xilinx/Kconfig" |
diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile index 99583d3df52f..13660a951437 100644 --- a/drivers/firmware/Makefile +++ b/drivers/firmware/Makefile | |||
@@ -31,5 +31,6 @@ obj-y += meson/ | |||
31 | obj-$(CONFIG_GOOGLE_FIRMWARE) += google/ | 31 | obj-$(CONFIG_GOOGLE_FIRMWARE) += google/ |
32 | obj-$(CONFIG_EFI) += efi/ | 32 | obj-$(CONFIG_EFI) += efi/ |
33 | obj-$(CONFIG_UEFI_CPER) += efi/ | 33 | obj-$(CONFIG_UEFI_CPER) += efi/ |
34 | obj-y += imx/ | ||
34 | obj-y += tegra/ | 35 | obj-y += tegra/ |
35 | obj-y += xilinx/ | 36 | obj-y += xilinx/ |
diff --git a/drivers/firmware/imx/Kconfig b/drivers/firmware/imx/Kconfig new file mode 100644 index 000000000000..b170c2851e48 --- /dev/null +++ b/drivers/firmware/imx/Kconfig | |||
@@ -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. | ||
diff --git a/drivers/firmware/imx/Makefile b/drivers/firmware/imx/Makefile new file mode 100644 index 000000000000..0ac04dfda8d4 --- /dev/null +++ b/drivers/firmware/imx/Makefile | |||
@@ -0,0 +1,2 @@ | |||
1 | # SPDX-License-Identifier: GPL-2.0 | ||
2 | obj-$(CONFIG_IMX_SCU) += imx-scu.o misc.o | ||
diff --git a/drivers/firmware/imx/imx-scu.c b/drivers/firmware/imx/imx-scu.c new file mode 100644 index 000000000000..2bb1a19c413f --- /dev/null +++ b/drivers/firmware/imx/imx-scu.c | |||
@@ -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"); | ||
diff --git a/drivers/firmware/imx/misc.c b/drivers/firmware/imx/misc.c new file mode 100644 index 000000000000..97f5424dbac9 --- /dev/null +++ b/drivers/firmware/imx/misc.c | |||
@@ -0,0 +1,99 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0+ | ||
2 | /* | ||
3 | * Copyright (C) 2016 Freescale Semiconductor, Inc. | ||
4 | * Copyright 2017~2018 NXP | ||
5 | * Author: Dong Aisheng <aisheng.dong@nxp.com> | ||
6 | * | ||
7 | * File containing client-side RPC functions for the MISC service. These | ||
8 | * function are ported to clients that communicate to the SC. | ||
9 | * | ||
10 | */ | ||
11 | |||
12 | #include <linux/firmware/imx/svc/misc.h> | ||
13 | |||
14 | struct imx_sc_msg_req_misc_set_ctrl { | ||
15 | struct imx_sc_rpc_msg hdr; | ||
16 | u32 ctrl; | ||
17 | u32 val; | ||
18 | u16 resource; | ||
19 | } __packed; | ||
20 | |||
21 | struct imx_sc_msg_req_misc_get_ctrl { | ||
22 | struct imx_sc_rpc_msg hdr; | ||
23 | u32 ctrl; | ||
24 | u16 resource; | ||
25 | } __packed; | ||
26 | |||
27 | struct imx_sc_msg_resp_misc_get_ctrl { | ||
28 | struct imx_sc_rpc_msg hdr; | ||
29 | u32 val; | ||
30 | } __packed; | ||
31 | |||
32 | /* | ||
33 | * This function sets a miscellaneous control value. | ||
34 | * | ||
35 | * @param[in] ipc IPC handle | ||
36 | * @param[in] resource resource the control is associated with | ||
37 | * @param[in] ctrl control to change | ||
38 | * @param[in] val value to apply to the control | ||
39 | * | ||
40 | * @return Returns 0 for success and < 0 for errors. | ||
41 | */ | ||
42 | |||
43 | int imx_sc_misc_set_control(struct imx_sc_ipc *ipc, u32 resource, | ||
44 | u8 ctrl, u32 val) | ||
45 | { | ||
46 | struct imx_sc_msg_req_misc_set_ctrl msg; | ||
47 | struct imx_sc_rpc_msg *hdr = &msg.hdr; | ||
48 | |||
49 | hdr->ver = IMX_SC_RPC_VERSION; | ||
50 | hdr->svc = (uint8_t)IMX_SC_RPC_SVC_MISC; | ||
51 | hdr->func = (uint8_t)IMX_SC_MISC_FUNC_SET_CONTROL; | ||
52 | hdr->size = 4; | ||
53 | |||
54 | msg.ctrl = ctrl; | ||
55 | msg.val = val; | ||
56 | msg.resource = resource; | ||
57 | |||
58 | return imx_scu_call_rpc(ipc, &msg, true); | ||
59 | } | ||
60 | EXPORT_SYMBOL(imx_sc_misc_set_control); | ||
61 | |||
62 | /* | ||
63 | * This function gets a miscellaneous control value. | ||
64 | * | ||
65 | * @param[in] ipc IPC handle | ||
66 | * @param[in] resource resource the control is associated with | ||
67 | * @param[in] ctrl control to get | ||
68 | * @param[out] val pointer to return the control value | ||
69 | * | ||
70 | * @return Returns 0 for success and < 0 for errors. | ||
71 | */ | ||
72 | |||
73 | int imx_sc_misc_get_control(struct imx_sc_ipc *ipc, u32 resource, | ||
74 | u8 ctrl, u32 *val) | ||
75 | { | ||
76 | struct imx_sc_msg_req_misc_get_ctrl msg; | ||
77 | struct imx_sc_msg_resp_misc_get_ctrl *resp; | ||
78 | struct imx_sc_rpc_msg *hdr = &msg.hdr; | ||
79 | int ret; | ||
80 | |||
81 | hdr->ver = IMX_SC_RPC_VERSION; | ||
82 | hdr->svc = (uint8_t)IMX_SC_RPC_SVC_MISC; | ||
83 | hdr->func = (uint8_t)IMX_SC_MISC_FUNC_GET_CONTROL; | ||
84 | hdr->size = 3; | ||
85 | |||
86 | msg.ctrl = ctrl; | ||
87 | msg.resource = resource; | ||
88 | |||
89 | ret = imx_scu_call_rpc(ipc, &msg, true); | ||
90 | if (ret) | ||
91 | return ret; | ||
92 | |||
93 | resp = (struct imx_sc_msg_resp_misc_get_ctrl *)&msg; | ||
94 | if (val != NULL) | ||
95 | *val = resp->val; | ||
96 | |||
97 | return 0; | ||
98 | } | ||
99 | EXPORT_SYMBOL(imx_sc_misc_get_control); | ||