88430299c9281ea5703df6421958b5494658e669
[platform/kernel/u-boot.git] / drivers / remoteproc / k3_system_controller.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Texas Instruments' K3 System Controller Driver
4  *
5  * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
6  *      Lokesh Vutla <lokeshvutla@ti.com>
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <remoteproc.h>
12 #include <errno.h>
13 #include <mailbox.h>
14 #include <dm/device_compat.h>
15 #include <linux/soc/ti/k3-sec-proxy.h>
16
17 #define K3_MSG_R5_TO_M3_M3FW                    0x8105
18 #define K3_MSG_M3_TO_R5_CERT_RESULT             0x8805
19 #define K3_MSG_M3_TO_R5_BOOT_NOTIFICATION       0x000A
20
21 #define K3_FLAGS_MSG_CERT_AUTH_PASS             0x555555
22 #define K3_FLAGS_MSG_CERT_AUTH_FAIL             0xffffff
23
24 /**
25  * struct k3_sysctrler_msg_hdr - Generic Header for Messages and responses.
26  * @cmd_id:     Message ID. One of K3_MSG_*
27  * @host_id:    Host ID of the message
28  * @seq_ne:     Message identifier indicating a transfer sequence.
29  * @flags:      Flags for the message.
30  */
31 struct k3_sysctrler_msg_hdr {
32         u16 cmd_id;
33         u8 host_id;
34         u8 seq_nr;
35         u32 flags;
36 } __packed;
37
38 /**
39  * struct k3_sysctrler_load_msg - Message format for Firmware loading
40  * @hdr:                Generic message hdr
41  * @buffer_address:     Address at which firmware is located.
42  * @buffer_size:        Size of the firmware.
43  */
44 struct k3_sysctrler_load_msg {
45         struct k3_sysctrler_msg_hdr hdr;
46         u32 buffer_address;
47         u32 buffer_size;
48 } __packed;
49
50 /**
51  * struct k3_sysctrler_boot_notification_msg - Message format for boot
52  *                                             notification
53  * @checksum:           Checksum for the entire message
54  * @reserved:           Reserved for future use.
55  * @hdr:                Generic message hdr
56  */
57 struct k3_sysctrler_boot_notification_msg {
58         u16 checksum;
59         u16 reserved;
60         struct k3_sysctrler_msg_hdr hdr;
61 } __packed;
62
63 /**
64  * struct k3_sysctrler_desc - Description of SoC integration.
65  * @host_id:    Host identifier representing the compute entity
66  * @max_rx_timeout_ms:  Timeout for communication with SoC (in Milliseconds)
67  * @max_msg_size: Maximum size of data per message that can be handled.
68  */
69 struct k3_sysctrler_desc {
70         u8 host_id;
71         int max_rx_timeout_us;
72         int max_msg_size;
73 };
74
75 /**
76  * struct k3_sysctrler_privdata - Structure representing System Controller data.
77  * @chan_tx:    Transmit mailbox channel
78  * @chan_rx:    Receive mailbox channel
79  * @desc:       SoC description for this instance
80  * @seq_nr:     Counter for number of messages sent.
81  */
82 struct k3_sysctrler_privdata {
83         struct mbox_chan chan_tx;
84         struct mbox_chan chan_rx;
85         struct k3_sysctrler_desc *desc;
86         u32 seq_nr;
87 };
88
89 static inline
90 void k3_sysctrler_load_msg_setup(struct k3_sysctrler_load_msg *fw,
91                                  struct k3_sysctrler_privdata *priv,
92                                  ulong addr, ulong size)
93 {
94         fw->hdr.cmd_id = K3_MSG_R5_TO_M3_M3FW;
95         fw->hdr.host_id = priv->desc->host_id;
96         fw->hdr.seq_nr = priv->seq_nr++;
97         fw->hdr.flags = 0x0;
98         fw->buffer_address = addr;
99         fw->buffer_size = size;
100 }
101
102 static int k3_sysctrler_load_response(u32 *buf)
103 {
104         struct k3_sysctrler_load_msg *fw;
105
106         fw = (struct k3_sysctrler_load_msg *)buf;
107
108         /* Check for proper response ID */
109         if (fw->hdr.cmd_id != K3_MSG_M3_TO_R5_CERT_RESULT) {
110                 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
111                         __func__, K3_MSG_M3_TO_R5_CERT_RESULT, fw->hdr.cmd_id);
112                 return -EINVAL;
113         }
114
115         /* Check for certificate authentication result */
116         if (fw->hdr.flags == K3_FLAGS_MSG_CERT_AUTH_FAIL) {
117                 dev_err(dev, "%s: Firmware certificate authentication failed\n",
118                         __func__);
119                 return -EINVAL;
120         } else if (fw->hdr.flags != K3_FLAGS_MSG_CERT_AUTH_PASS) {
121                 dev_err(dev, "%s: Firmware Load response Invalid %d\n",
122                         __func__, fw->hdr.flags);
123                 return -EINVAL;
124         }
125
126         debug("%s: Firmware authentication passed\n", __func__);
127
128         return 0;
129 }
130
131 static int k3_sysctrler_boot_notification_response(u32 *buf)
132 {
133         struct k3_sysctrler_boot_notification_msg *boot;
134
135         boot = (struct k3_sysctrler_boot_notification_msg *)buf;
136
137         /* ToDo: Verify checksum */
138
139         /* Check for proper response ID */
140         if (boot->hdr.cmd_id != K3_MSG_M3_TO_R5_BOOT_NOTIFICATION) {
141                 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
142                         __func__, K3_MSG_M3_TO_R5_BOOT_NOTIFICATION,
143                         boot->hdr.cmd_id);
144                 return -EINVAL;
145         }
146
147         debug("%s: Boot notification received\n", __func__);
148
149         return 0;
150 }
151
152 /**
153  * k3_sysctrler_load() - Loadup the K3 remote processor
154  * @dev:        corresponding K3 remote processor device
155  * @addr:       Address in memory where image binary is stored
156  * @size:       Size in bytes of the image binary
157  *
158  * Return: 0 if all goes good, else appropriate error message.
159  */
160 static int k3_sysctrler_load(struct udevice *dev, ulong addr, ulong size)
161 {
162         struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
163         struct k3_sysctrler_load_msg firmware;
164         struct k3_sec_proxy_msg msg;
165         int ret;
166
167         debug("%s: Loading binary from 0x%08lX, size 0x%08lX\n",
168               __func__, addr, size);
169
170         memset(&firmware, 0, sizeof(firmware));
171         memset(&msg, 0, sizeof(msg));
172
173         /* Setup the message */
174         k3_sysctrler_load_msg_setup(&firmware, priv, addr, size);
175         msg.len = sizeof(firmware);
176         msg.buf = (u32 *)&firmware;
177
178         /* Send the message */
179         ret = mbox_send(&priv->chan_tx, &msg);
180         if (ret) {
181                 dev_err(dev, "%s: Firmware Loading failed. ret = %d\n",
182                         __func__, ret);
183                 return ret;
184         }
185
186         /* Receive the response */
187         ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
188         if (ret) {
189                 dev_err(dev, "%s: Firmware Load response failed. ret = %d\n",
190                         __func__, ret);
191                 return ret;
192         }
193
194         /* Process the response */
195         ret = k3_sysctrler_load_response(msg.buf);
196         if (ret)
197                 return ret;
198
199         debug("%s: Firmware Loaded successfully on dev %s\n",
200               __func__, dev->name);
201
202         return 0;
203 }
204
205 /**
206  * k3_sysctrler_start() - Start the remote processor
207  *              Note that while technically the K3 system controller starts up
208  *              automatically after its firmware got loaded we still want to
209  *              utilize the rproc start operation for other startup-related
210  *              tasks.
211  * @dev:        device to operate upon
212  *
213  * Return: 0 if all went ok, else return appropriate error
214  */
215 static int k3_sysctrler_start(struct udevice *dev)
216 {
217         struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
218         struct k3_sec_proxy_msg msg;
219         int ret;
220
221         debug("%s(dev=%p)\n", __func__, dev);
222
223         /* Receive the boot notification. Note that it is sent only once. */
224         ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
225         if (ret) {
226                 dev_err(dev, "%s: Boot Notification response failed. ret = %d\n",
227                         __func__, ret);
228                 return ret;
229         }
230
231         /* Process the response */
232         ret = k3_sysctrler_boot_notification_response(msg.buf);
233         if (ret)
234                 return ret;
235
236         debug("%s: Boot notification received successfully on dev %s\n",
237               __func__, dev->name);
238
239         return 0;
240 }
241
242 static const struct dm_rproc_ops k3_sysctrler_ops = {
243         .load = k3_sysctrler_load,
244         .start = k3_sysctrler_start,
245 };
246
247 /**
248  * k3_of_to_priv() - generate private data from device tree
249  * @dev:        corresponding k3 remote processor device
250  * @priv:       pointer to driver specific private data
251  *
252  * Return: 0 if all goes good, else appropriate error message.
253  */
254 static int k3_of_to_priv(struct udevice *dev,
255                          struct k3_sysctrler_privdata *priv)
256 {
257         int ret;
258
259         ret = mbox_get_by_name(dev, "tx", &priv->chan_tx);
260         if (ret) {
261                 dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
262                         __func__, ret);
263                 return ret;
264         }
265
266         ret = mbox_get_by_name(dev, "rx", &priv->chan_rx);
267         if (ret) {
268                 dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
269                         __func__, ret);
270                 return ret;
271         }
272
273         return 0;
274 }
275
276 /**
277  * k3_sysctrler_probe() - Basic probe
278  * @dev:        corresponding k3 remote processor device
279  *
280  * Return: 0 if all goes good, else appropriate error message.
281  */
282 static int k3_sysctrler_probe(struct udevice *dev)
283 {
284         struct k3_sysctrler_privdata *priv;
285         int ret;
286
287         debug("%s(dev=%p)\n", __func__, dev);
288
289         priv = dev_get_priv(dev);
290
291         ret = k3_of_to_priv(dev, priv);
292         if (ret) {
293                 dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
294                 return ret;
295         }
296
297         priv->desc = (void *)dev_get_driver_data(dev);
298         priv->seq_nr = 0;
299
300         return 0;
301 }
302
303 static const struct k3_sysctrler_desc k3_sysctrler_am654_desc = {
304         .host_id = 4,                           /* HOST_ID_R5_1 */
305         .max_rx_timeout_us = 800000,
306         .max_msg_size = 60,
307 };
308
309 static const struct udevice_id k3_sysctrler_ids[] = {
310         {
311                 .compatible = "ti,am654-system-controller",
312                 .data = (ulong)&k3_sysctrler_am654_desc,
313         },
314         {}
315 };
316
317 U_BOOT_DRIVER(k3_sysctrler) = {
318         .name = "k3_system_controller",
319         .of_match = k3_sysctrler_ids,
320         .id = UCLASS_REMOTEPROC,
321         .ops = &k3_sysctrler_ops,
322         .probe = k3_sysctrler_probe,
323         .priv_auto_alloc_size = sizeof(struct k3_sysctrler_privdata),
324 };